0

I have this script

# check lost+found folders
function chklnf {
#DOC echo "chklnf                  - list any files in lost+found folders on mounted devices"
x="lost+found/"
if [ "$EUID" != 0 ] ;then
  echo "Please enter sudoers password to allow access to $x folders."
  sudo echo "Thank you..."
fi
for p in $(mount | sed -nre 's,^/dev/.+ on ([^ ]+).*$,\1,p')
  do
    if [ -d "$p/$x" ] ;then
      echo -n "--- $p/$x --- "
      sudo ls -l "$p/$x"
    fi
  done
}

I'd like to have this run once at the end of .bash_profile, but;
it is inconvenient to have to type in the passwd for every new shell prompt.
So: is it possible to "get rid of" that for the "ls -l" in there?

Alternatively:
how to detect if "sudo mode" is already present? (ref: "$EUID" in the script)


After snipping the function-def, saving as file /home/hannu/bin/checklostfound, and adding the last line /etc/sudoers (shown below)
$ sudo cat /etc/sudoers
[sudo] password for hannu: ****************           
#
# This file MUST be edited with the 'visudo' command as root.
#

... {SNIP}

# Special single script hannu ALL=(root) NOPASSWD: /home/hannu/bin/checklostfound

$ ls -l /home/hannu/bin/checklostfound -rwx------ 1 hannu hannu 526 sep 24 16:30 /home/hannu/bin/checklostfound

$ sudo ~/bin/checklostfound --- //lost+found/ --- total 0

$

In the end, with adjustment for $p having an / at the end:

$ cat /home/hannu/bin/checklostfound 
#!/bin/env bash
# check lost+found folders

#DOC echo "chklnf                  - list any files in lost+found folders on mounted devices"
x="lost+found/"
if [ "$EUID" != 0 ] ;then
  echo "Please enter sudoers password to allow access to $x folders."
  sudo echo "Thank you..."
fi
for p in $(mount | sed -nre 's,^/dev/.+ on ([^ ]+).*$,\1,p')
  do
    sep="/"
    if [ "${p: -1}" = "/" ]; then sep=""; fi
    if [ -d "$p$sep$x" ] ;then
      echo -n "--- $p$sep$x --- "
      sudo ls -l "$p$sep$x"
    fi
  done


$ sudo /home/hannu/bin/checklostfound 
-- /lost+found/ --- total 0
--- /media/hannu/Data8/lost+found/ --- total 0

$ /home/hannu/bin/checklostfound 
Please enter sudoers password to allow access to lost+found/ folders.
[sudo] password for hannu:           
Thank you...
--- /lost+found/ --- total 0
--- /media/hannu/Data8/lost+found/ --- total 0
$

1 Answer 1

0

Convert your function to a standalone script in /usr/local/bin. Now you have a much simpler problem of needing to grant passwordless 'sudo' access to just that one fixed command, which can be done through /etc/sudoers:

hannu ALL=(root) NOPASSWD: /usr/local/bin/checklostfound

how to detect if "sudo" mode is already present for current user? (ref: "$EUID" in the script)

"sudo mode" literally means the current user is not the original user anymore, it means root is now the "current user".

Your comparison syntax is wrong – the spaces around = are not optional. It needs to be:

if [ "$EUID" = 0 ]; then
6
  • Right, removed "for current user" there; Not always using entirely proper wording.
    – Hannu
    Commented Sep 24, 2023 at 13:08
  • see: superuser.com: How can I allow all users to run a given command via sudo?
    – Luuk
    Commented Sep 24, 2023 at 13:15
  • Hmm... something is still missing, added <HR> followed by related info
    – Hannu
    Commented Sep 24, 2023 at 15:00
  • Did you run sudo /usr/local/bin/checklostfound? That's what the sudoers config allows. Commented Sep 24, 2023 at 15:04
  • AHH. you STILL have to use sudo! But without the passwd. That was NOT stated above ;-)
    – Hannu
    Commented Sep 24, 2023 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .