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 $