Check my Script and Say what Grinds Your Gears


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check my Script and Say what Grinds Your Gears
# 1  
Check my Script and Say what Grinds Your Gears

Image

This is a script that I, this unix forum and stackoverflow come up with. I would like to get some feedback about it and improve it little bit if possible for even more readability and better working. OR get some shaming hilarious opinion.
Quote:
What this script does?
It checks whether this Script is ran with Bash shell.
If it did not, the notice will be echo'ed.
And the Script rerun with Bash.

Later:
It will check whether this Script is ran by the User that can execute Sudo directly or will be promped for a password.
The notice will be echo'ed.



Little bit more Later:

It will check if the User that executes this Script ran the Script with Sudo.
If Not, this script will be re-run with Sudo and Bash.
Code:
#!/bin/bash
currentShell=$(readlink /proc/$$/exe);
if [ "$currentShell" != "/bin/bash" ]
then 
        echo 'The script is ran by different shell, not Bash shell, be cautious next time.';
        echo 'I will rerun this script with Bash shell for You, to avoid unusual behaviour';
        exec bash "$0" "$@";
fi

declare sudoPasswordCache=$((sudo -n true) 2>&1);
if [ "$sudoPasswordCache" = "sudo: a password is required" ];
then 
        echo ""
        echo 'This script is required to be ran as root user.';
        echo 'You will be asked for a password';
        #else echo 'You have a cached password in place, no need for password.'
fi

if [ "$EUID" -ne 0 ];
then
        exec sudo bash "$0" "$@";
        echo "exec failed" >&2
        exit 1;
fi


Last edited by boqsc; 11-15-2018 at 02:22 PM..
# 2  
Not everyone has readlink. You can check the SHELL variable instead, it's quite standard.

Use [[ "$VAR1" == "value" ]] as single-equals and single square-braces are now archaic.

Automatically rerunning it with BASH is begging for an infinite loop if anything weird happens. You also didn't check if that exec failed.

Error messages should go to stderr, a la
Code:
echo 'The script is ran by different shell, not Bash shell, be cautious next time.' >&2

Otherwise, people may find big flat error messages in their flatfiles whenever they redirect the output.

You don't need to and probably shouldn't be checking the text output of sudo -- just get its return value. There's also no reason to use a sub-shell, or a sub-sub-shell within that sub-shell. You can put it right into the if-statement as if it belongs there, which it does, with an ! in front to invert the return value.

Code:
if ! sudo -n true </dev/null 2>/dev/null
then
        echo 'This script is required to be ran as root user.' >&2
        echo 'You will be asked for a password.' >&2
fi

You don't need to end lines with ;
# 3  
For that matter, why does the shell need to be bash, when you're always going to be doing an exec sudo bash anyway?
# 4  
$SHELL only indicates the default interpreter not the one currently executing the script.

I'd try:
Code:
[[ -z "$BASH_VERSION" ]]

exec sudo bash is not executed if user is already root
# 5  
Quote:
Originally Posted by Chubler_XL
$SHELL only indicates the default interpreter not the one currently executing the script.

I'd try:
Code:
[[ -z "$BASH_VERSION" ]]

exec sudo bash is not executed if user is already root
instead of $SHELL, I'd use $0 in the script itself instead of the readlink approach.
# 6  
Code:
$ echo 'echo $0' > vgersh99
$ ./vgersh99 testing
./vgersh99

# 7  
Quote:
Originally Posted by Chubler_XL
Code:
$ echo 'echo $0' > vgersh99
$ ./vgersh99 testing
./vgersh99

I stand corrected - I was thinking of something else - thanks!
Another option a bit more OS-agnostic would be: ps -p $$

Last edited by vgersh99; 11-15-2018 at 06:40 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #613
Difficulty: Medium
Examples of complex numbers in Python are (-5+4j) and (2.3 - 4.6j).
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

2. Programming

Check This Script

Hi guys, I am sending the script i created..plzzz check it correct if any mistakes...:). Plz reply early by today.. here i want to find the files based on date , if files are avaliable i want to send a mail to the user. here there are a total of 43 files. they are constant. Here is... (0 Replies)
Discussion started by: apple2685
0 Replies

3. Shell Programming and Scripting

Memory check script

Hi all, OS: Solaris 10 I'm trying to put together a script to check memory utilisation. The command I'm running is: ps -ef -o pmem -o pid -o rss -o vsz -o args -o user | grep 2 | grep -v "VSZ COMMAND" | sort | tail -10 | sort -r And the output looks something like this: ... (9 Replies)
Discussion started by: d-train
9 Replies

4. Shell Programming and Scripting

script to check if another script is running and if so, then sleep for sometime and check again

Hi, I am a unix newbie. I need to write a script to check wheteher another script is still running. If it is, then sleep for 30m and then check again if the script is running. If the script has stopped running then, I need to come out of the loop. I am using RHEL 5.2 (2 Replies)
Discussion started by: mathews
2 Replies

5. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

6. Shell Programming and Scripting

Script to check connectivity

I want to write a script to check if a unix box say abc.tdc.cin.net can be connected or not on certain port say 22. right know i have to telnet them manually from DOS prompt and if it is successful then isay it is connected. Also to check Database connectivity I am using tnsping From DOS prompt.... (3 Replies)
Discussion started by: kukretiabhi13
3 Replies

7. Shell Programming and Scripting

please check this script.

please check this script and let me know.wher eis the issue. #!/bin/ksh set -A logs AAA BB CCC DDD EE FFF set -A ltime 7 60 7 7 7 60 set i=1 while } ] do find /home/logs -name "*"\${logs}"\*.log" -mtime ${l time} -type f -ls let i=$i+1 done (3 Replies)
Discussion started by: sab_sree
3 Replies

8. Shell Programming and Scripting

check in unix shell script so that no one is able to run the script manually

I want to create an automated script which is called by another maually executed script. The condition is that the no one should be able to manually execute the automated script. The automated script can be on the same machine or it can be on a remote machine. Can any one suggest a check in the... (1 Reply)
Discussion started by: adi_bang76
1 Replies

9. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies

Featured Tech Videos