Difference between revisions of "Bash Shell Script Examples"
(Created page with "== Add Pause Prompt In a Shell Script == Use "read". There is no pause command under bash shell. #!/bin/bash echo $1 read -p "Press [Enter] key to continue." The sampl...") |
m |
||
(10 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | == Add Pause Prompt In a Shell Script == | + | === three expression control loop === |
+ | |||
+ | #!/bin/bash | ||
+ | for (( c=1; c<=5; c++ )) | ||
+ | do | ||
+ | echo "Welcome $c times" | ||
+ | done | ||
+ | |||
+ | === Add Pause Prompt In a Shell Script === | ||
Use "read". There is no pause command under bash shell. | Use "read". There is no pause command under bash shell. | ||
#!/bin/bash | #!/bin/bash | ||
− | |||
read -p "Press [Enter] key to continue." | read -p "Press [Enter] key to continue." | ||
+ | exit 0 | ||
The sample script above shows what is entered as a command line parameter and then pauses, waiting for the user to press the ENTER key to continue. This creates a PAUSE. | The sample script above shows what is entered as a command line parameter and then pauses, waiting for the user to press the ENTER key to continue. This creates a PAUSE. | ||
+ | === Require command line parameters === | ||
+ | |||
+ | see example: | ||
+ | if [ -z $1 ] || [ -z $2 ] ; then | ||
+ | echo "newuser: too few arguments" | ||
+ | echo "Usage: newuser [username] [uid]" | ||
+ | echo "Adds a new robotz.com customer, for use by admin@robotz.com only." | ||
+ | else | ||
+ | useradd -r $1 -d /home/$1 -m -n -u $2 | ||
+ | fi | ||
+ | exit 0 | ||
+ | |||
+ | ===Stopwatch=== | ||
+ | <nowiki>BEGIN=$(date +%s)</nowiki> | ||
+ | <nowiki></nowiki> | ||
+ | <nowiki>echo Starting Stopwatch...</nowiki> | ||
+ | <nowiki>echo Press Q to exit.</nowiki> | ||
+ | <nowiki></nowiki> | ||
+ | <nowiki>while true; do</nowiki> | ||
+ | <nowiki> NOW=$(date +%s)</nowiki> | ||
+ | <nowiki> let DIFF=$(($NOW - $BEGIN))</nowiki> | ||
+ | <nowiki> let MINS=$(($DIFF / 60))</nowiki> | ||
+ | <nowiki> let SECS=$(($DIFF % 60))</nowiki> | ||
+ | <nowiki> let HOURS=$(($DIFF / 3600))</nowiki> | ||
+ | <nowiki> let DAYS=$(($DIFF / 86400))</nowiki> | ||
+ | <nowiki></nowiki> | ||
+ | <nowiki> # \r is a "carriage return" - returns cursor to start of line</nowiki> | ||
+ | <nowiki> printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS</nowiki> | ||
+ | <nowiki></nowiki> | ||
+ | <nowiki># In the following line -t for timeout, -N for just 1 character</nowiki> | ||
+ | <nowiki> read -t 0.25 -N 1 input</nowiki> | ||
+ | <nowiki> if [[ $input = "q" ]] || [[ $input = "Q" ]]; then</nowiki> | ||
+ | <nowiki># The following line is for the prompt to appear on a new line.</nowiki> | ||
+ | <nowiki> echo</nowiki> | ||
+ | <nowiki> break </nowiki> | ||
+ | <nowiki> fi</nowiki> | ||
+ | <nowiki>done</nowiki> | ||
+ | |||
+ | === Press any key to continue === | ||
+ | read -n 1 -s -r -p "Press any key to continue" | ||
+ | |||
+ | === DateTime Code === | ||
+ | #!/bin/bash | ||
+ | PDATE=`date '+%Y%m%d%H%M%S'` | ||
+ | echo $PDATE | ||
+ | |||
+ | === Detach Command and Run in Background === | ||
+ | |||
+ | So if the terminal window is closed, the command continues to execute. | ||
+ | |||
+ | You can append an "&" to the end of the command. This detaches the command from stdin. The & ampersand character is placed right of a space at the end of the command. Keep in mind that the command’s process is still managed by the shell and stdout and stderr are still attached. In some cases the process will be terminated if the shell session is closed. It depends on how the process was written. | ||
+ | |||
+ | When you append the "&" at the end you are able to use the command prompt again, however, output from the process is displayed in the same terminal. You can redirect output to a text file or /dev/null. | ||
+ | |||
+ | processname &> /dev/null & | ||
+ | |||
+ | Now you have your command prompt back and will receive no messages, for the most part. | ||
+ | |||
+ | To see the process running look at the jobs | ||
+ | |||
+ | jobs | ||
+ | |||
+ | You will see it displayed. You can detach it with the "disown" command. | ||
+ | |||
+ | Now to deal with HUP. Use "nohup." | ||
+ | |||
+ | === Terminate execution of script on command error === | ||
+ | |||
+ | While your shell script executes commands one might return a non-zero value. At the top of your shell script use | ||
+ | set -e | ||
+ | This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list. | ||
+ | |||
+ | Works with redirects too, such as | ||
+ | command > output.txt | ||
+ | |||
+ | Does not work with pipes | ||
+ | command | anothercommand parameter | ||
+ | |||
+ | The set -e directive is sometimes insufficient for example if you have pipes. If your script contains commands with pipes then include | ||
+ | set -e | ||
+ | set -o pipefail | ||
+ | |||
+ | Rather than the entire script under the influence of set -e you can use an exit on non-zero just on specific command lines in the script | ||
+ | command || exit 1 | ||
+ | |||
+ | Or say you use to use a script wide set -e and exclude a particular line from causing the script to exit if that one particular command fails | ||
+ | badcommand || true | ||
+ | Even if badcommand exits non-zero the script will go on, while set -e was specified at the top. The || true will make the command pipeline have a true return value even if the command fails so the the -e option will not kill the script. | ||
+ | |||
+ | On errors, you can also use 'trap' with the pseudo-signal ERR | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === See Also... === | ||
+ | * [[Advanced Shell Operations]] - The 2001 shell script example cookbook | ||
+ | * [[How to write a shell script]] - Read before looking at examples | ||
+ | * [[Regular Expressions Tutorial]] - RegEx is the real power in the shell script | ||
[[Category:Computer_Technology]] | [[Category:Computer_Technology]] | ||
[[Category:Linux]] | [[Category:Linux]] |
Latest revision as of 11:48, 25 February 2021
Contents
three expression control loop
#!/bin/bash for (( c=1; c<=5; c++ )) do echo "Welcome $c times" done
Add Pause Prompt In a Shell Script
Use "read". There is no pause command under bash shell.
#!/bin/bash read -p "Press [Enter] key to continue." exit 0
The sample script above shows what is entered as a command line parameter and then pauses, waiting for the user to press the ENTER key to continue. This creates a PAUSE.
Require command line parameters
see example:
if [ -z $1 ] || [ -z $2 ] ; then echo "newuser: too few arguments" echo "Usage: newuser [username] [uid]" echo "Adds a new robotz.com customer, for use by admin@robotz.com only." else useradd -r $1 -d /home/$1 -m -n -u $2 fi exit 0
Stopwatch
BEGIN=$(date +%s) echo Starting Stopwatch... echo Press Q to exit. while true; do NOW=$(date +%s) let DIFF=$(($NOW - $BEGIN)) let MINS=$(($DIFF / 60)) let SECS=$(($DIFF % 60)) let HOURS=$(($DIFF / 3600)) let DAYS=$(($DIFF / 86400)) # \r is a "carriage return" - returns cursor to start of line printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS # In the following line -t for timeout, -N for just 1 character read -t 0.25 -N 1 input if [[ $input = "q" ]] || [[ $input = "Q" ]]; then # The following line is for the prompt to appear on a new line. echo break fi done
Press any key to continue
read -n 1 -s -r -p "Press any key to continue"
DateTime Code
#!/bin/bash PDATE=`date '+%Y%m%d%H%M%S'` echo $PDATE
Detach Command and Run in Background
So if the terminal window is closed, the command continues to execute.
You can append an "&" to the end of the command. This detaches the command from stdin. The & ampersand character is placed right of a space at the end of the command. Keep in mind that the command’s process is still managed by the shell and stdout and stderr are still attached. In some cases the process will be terminated if the shell session is closed. It depends on how the process was written.
When you append the "&" at the end you are able to use the command prompt again, however, output from the process is displayed in the same terminal. You can redirect output to a text file or /dev/null.
processname &> /dev/null &
Now you have your command prompt back and will receive no messages, for the most part.
To see the process running look at the jobs
jobs
You will see it displayed. You can detach it with the "disown" command.
Now to deal with HUP. Use "nohup."
Terminate execution of script on command error
While your shell script executes commands one might return a non-zero value. At the top of your shell script use
set -e
This will cause the shell to exit immediately if a simple command exits with a nonzero exit value. A simple command is any command not part of an if, while, or until test, or part of an && or || list.
Works with redirects too, such as
command > output.txt
Does not work with pipes
command | anothercommand parameter
The set -e directive is sometimes insufficient for example if you have pipes. If your script contains commands with pipes then include
set -e set -o pipefail
Rather than the entire script under the influence of set -e you can use an exit on non-zero just on specific command lines in the script
command || exit 1
Or say you use to use a script wide set -e and exclude a particular line from causing the script to exit if that one particular command fails
badcommand || true
Even if badcommand exits non-zero the script will go on, while set -e was specified at the top. The || true will make the command pipeline have a true return value even if the command fails so the the -e option will not kill the script.
On errors, you can also use 'trap' with the pseudo-signal ERR
See Also...
- Advanced Shell Operations - The 2001 shell script example cookbook
- How to write a shell script - Read before looking at examples
- Regular Expressions Tutorial - RegEx is the real power in the shell script