Sponsored Content
Top Forums Shell Programming and Scripting Check my Script and Say what Grinds Your Gears Post 303025935 by boqsc on Thursday 15th of November 2018 12:04:26 PM
Old 11-15-2018
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 01:22 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

script for netmask check

I want develop a script which should also check validity of netmask. e.g. /etc/netmasks 10.15.20.30 255.255.255.224 How can I check which IP adresses are valid for this netmask? I think the best is use logical operations. 224 is 1 1 1 0 0 0 0 0 so is valid from 10.15.20.31... (2 Replies)
Discussion started by: rhacodactylus
2 Replies

2. 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

3. 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

4. Shell Programming and Scripting

check my first shell script

I have written the below script to determine whether a string is palindrome or not ? But its not working, any help to debug it ? I am new to this forum but when I searched for my question, I found that many people refused to answer this question thinking that its Homework question, Therefore I... (2 Replies)
Discussion started by: gridview
2 Replies

5. 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

6. 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

7. 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

8. 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

9. 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
SCRIPT(1)						    BSD General Commands Manual 						 SCRIPT(1)

NAME
script -- make typescript of terminal session SYNOPSIS
script [-a] [-c COMMAND] [-f] [-q] [-t] [file] DESCRIPTION
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript. Options: -a Append the output to file or typescript, retaining the prior contents. -c COMMAND Run the COMMAND rather than an interactive shell. This makes it easy for a script to capture the output of a program that behaves differently when its stdout is not a tty. -f Flush output after each write. This is nice for telecooperation: One person does `mkfifo foo; script -f foo' and another can super- vise real-time what is being done using `cat foo'. -q Be quiet. -t Output timing data to standard error. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can be used to replay typescripts with realistic typing and output delays. The script ends when the forked shell exits (a control-D to exit the Bourne shell (sh(1)), and exit, logout or control-d (if ignoreeof is not set) for the C-shell, csh(1)). Certain interactive commands, such as vi(1), create garbage in the typescript file. Script works best with commands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal. ENVIRONMENT
The following environment variable is utilized by script: SHELL If the variable SHELL exists, the shell forked by script will be that shell. If SHELL is not set, the Bourne shell is assumed. (Most shells set this variable automatically). SEE ALSO
csh(1) (for the history mechanism), scriptreplay(1). HISTORY
The script command appeared in 3.0BSD. BUGS
Script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects. AVAILABILITY
The script command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/. Linux July 30, 2000 Linux
All times are GMT -4. The time now is 05:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy