Shell Script
From BC$ MobileTV Wiki
(Redirected from Bash)
A Shell Script is a system-level control program which enables certain operations to be automated.
Contents
Shebang
The "shebang" or "opening header" is a directive which should appear at the top of a particular type of Shell script and location. It should be any ONE of the following:
#!/bin/sh #!/bin/bash #!/usr/bin/perl #!/usr/bin/tcl #!/bin/sed -f #!/usr/awk -f
Commands
run
./program.sh [ARGS]
Run shell script As root:
sudo sh program.sh [ARGS]
locate
locate <something>
ex:
locate tomcat
list all known files/folders which contain the name "tomcat"
grep
ps ax | grep <something>
ex:
ps ax | grep VRD
list running processes which start with "VRD"
awk
nohup
nohup <something>
ex:
nohup VRD-proxy &
run VRD-Proxy application with "no hang-ups" (even after shell closes) [3]
trap
Setup a cleanup function to be triggered upon script exit:
__cleanup () { -f "MyProcess.lock" && rm "MyProcess.lock" } # ... # bunch of other commands and stuff # ... trap __cleanup EXIT
Environment Variables
- How to set environment variable for everyone under my linux system?: https://stackoverflow.com/questions/1641477/how-to-set-environment-variable-for-everyone-under-my-linux-system (some techs say to put it in "/etc/environment" others say "/etc/profile.d/" and yet more say "~/.bashrc" as an environment variable)
- How To Read and Set Environmental and Shell Variables on a Linux VPS: https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps
Resources
- Short but Simple Shell Script cheat-sheet: http://www.cheat-sheets.org/saved-copy/shellcheatsheet.pdf
- Bash Shell Scripting Cheat Sheet: http://riceball.com/d/content/shell-scripting-cheat-sheet[6][7]
- Shell Scripting - Syntax Cheat Sheet: http://steve-parker.org/sh/cheatsheet.pdf[8][9]
- Advanced Shell Scripting guide: http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/refcards.html
- Shellshock - Better 'bash' shell security patches now available: http://www.zdnet.com/shellshock-better-bash-patches-now-available-7000034115/
- Bash Guide for Beginners: http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html
- Advanced Bash-Scripting Guide: http://www.tldp.org/LDP/abs/html/ (An in-depth exploration of the art of shell scripting)
- Shelldorado: http://shelldorado.com/ (UNIX shell scripting resource)
- Bash Scripting examples: http://programmingexamples.wikidot.com/bash-scripting
Tutorials
- Writing Shell Database and File Backup Scripts for OS X: http://www.noendpress.com/vroman/shellscripting/index.php
- Basic sample shell script: http://www.go2linux.org/starting-with-shell-script
- UNIX & Linux Shell Scripting Tutorial: http://www.dreamsyssoft.com/sp_ifelse.jsp
- Unix / Linux - Using Shell Variables: https://www.tutorialspoint.com/unix/unix-using-variables.htm
- Working with Variables in Shell Scripts: http://www.informit.com/articles/article.aspx?p=29246
- Working with script variables, including command line arguments: http://pangea.stanford.edu/computerinfo/unix/programming/shell/variables.html
- How to Pass Arguments to Shell Script: http://bashscript.blogspot.com/2009/11/how-to-pass-arguments-to-shell-script.html[10]
- Shell Script to get user input: http://www.thelinuxblog.com/shell-script-get-user-input/
- Handling Command Line Arguments: http://www.shelldorado.com/goodcoding/cmdargs.html
- How do you take a single line of input from the user in a shell script?: http://www.cyberciti.biz/faq/read-input-from-the-user-in-a-shell-script/
- How to set a variable to the output from a command in Bash?: https://stackoverflow.com/questions/4651437/how-to-set-a-variable-to-the-output-from-a-command-in-bash
- Set shell script Variable to output of command: https://stackoverflow.com/questions/38641757/set-shell-script-variable-to-output-of-command
- How do I trim leading and trailing whitespace from each line of some output?: https://unix.stackexchange.com/questions/102008/how-do-i-trim-leading-and-trailing-whitespace-from-each-line-of-some-output
- CURL used in shell: https://superuser.com/questions/760645/curl-used-in-shell (checking for status to ensure program closes correctly)
- Shell Script - syntax error near unexpected token `else': https://unix.stackexchange.com/questions/87776/shell-script-syntax-error-near-unexpected-token-else (you must either put "if [ ... ]; then" for same-line or newline between the "if [ ... ]" and "then")
- How To Untar a Tarball .tar or .tar.gz File In Linux Shell Command: http://www.wallpaperama.com/disp-post26.html
- I/O With Files That Aren't Files: http://java.dzone.com/articles/io-files-arent-files (using PIPEs to temporarily open/cache files and streams for processing)
- Passing arguments to a shell script: http://osr507doc.sco.com/en/OSUserG/_Passing_to_shell_script.html[11][12]
- Passing named arguments to shell scripts: https://unix.stackexchange.com/questions/129391/passing-named-arguments-to-shell-scripts
- Jenkins shell script does not receive the new value of a job parameter: https://stackoverflow.com/questions/35085788/jenkins-shell-script-does-not-receive-the-new-value-of-a-job-parameter[13]
- Bash -- Exit and cleanup on error: https://stackoverflow.com/questions/36335186/bash-exit-and-cleanup-on-error[14][15][16]
- How to run a shell script at startup: https://stackoverflow.com/questions/12973777/how-to-run-a-shell-script-at-startup
- How to save the output of a shell command to a variable: https://samindaw.wordpress.com/2009/02/25/how-to-save-the-output-of-a-shell-command-to-a-variable/
- How to increment a variable in bash?: https://askubuntu.com/questions/385528/how-to-increment-a-variable-in-bash
- HowTo -- Check If a String Exists: https://www.shellhacks.com/check-if-string-exists/
- Bash – Check If Two Strings are Equal: https://tecadmin.net/tutorial/bash/examples/check-if-two-strings-are-equal/
- How to represent multiple conditions in a shell if statement?: https://stackoverflow.com/questions/3826425/how-to-represent-multiple-conditions-in-a-shell-if-statement
- How do I make a batch file terminate upon encountering an error?: https://stackoverflow.com/questions/734598/how-do-i-make-a-batch-file-terminate-upon-encountering-an-error/8965092#8965092
- How to Use "Case Statements" in Bash Scripts: https://www.howtogeek.com/766978/how-to-use-case-statements-in-bash-scripts/
- How to Use Brace Expansion in Linux’s Bash Shell: https://www.howtogeek.com/725657/how-to-use-brace-expansion-in-linuxs-bash-shell/
- How to check if a string contains a substring in Bash: https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash
- Split string into array (on delimiter) Shellscript: https://stackoverflow.com/questions/10130280/split-string-into-array-shellscript
- How to find the length of an array in shell?: https://stackoverflow.com/questions/1886374/how-to-find-the-length-of-an-array-in-shell
- Bash split string into array using 4 simple methods: https://www.golinuxcloud.com/bash-split-string-into-array-linux/
External Links
- wikipedia: Shell script
- wikipedia: Bash (Unix shell)
- wikipedia: Comparison of command shells
- Bash Guide for Beginners: http://tldp.org/LDP/Bash-Beginners-Guide/html/
- An example of Shell Scripting using the Simpson family: http://www.joedog.org/articles-cheat-sheet/
- Advanced Bash-Scripting Guide -- An in-depth exploration of the art of shell scripting: http://www.tldp.org/LDP/abs/html/
- Rute (terminal) User's Tutorial and Exposition: http://rute.2038bug.com/index.html.gz
- "/bin/sh : bad interpreter: No such file or directory": https://www.raspberrypi.org/forums/viewtopic.php?t=77127 (likely requires a run through "dos2unix" CLI tool if you edited it in Windows and are trying to run in Linux/Unix)
- Gracefully Close an Application from the Command Line in Mac OS X: http://osxdaily.com/2014/09/05/gracefully-quit-application-command-line/
- How to kill a script running in terminal, without closing terminal (Ctrl + C doesn't work)?: https://askubuntu.com/questions/520107/how-to-kill-a-script-running-in-terminal-without-closing-terminal-ctrl-c-doe
- Exit terminal after running a bash script: https://askubuntu.com/questions/611648/exit-terminal-after-running-a-bash-script
- How do I exit or cancel a bad bash command?: https://unix.stackexchange.com/questions/45646/how-do-i-exit-or-cancel-a-bad-bash-command
- Bash script exit cleanup: https://stackoverflow.com/questions/24151726/bash-script-exit-cleanup
- An introduction to services, runlevels, and rc.d scripts: https://www.linux.com/news/introduction-services-runlevels-and-rcd-scripts
- How to run a shell script at startup: https://stackoverflow.com/questions/12973777/how-to-run-a-shell-script-at-startup
- Run shell script on startup or boot: https://askubuntu.com/questions/108800/run-shell-script-on-startup-or-boot
- How to execute a shell script on startup?: https://superuser.com/questions/126106/how-to-execute-a-shell-script-on-startup
- Looping through the content of a file in Bash?: https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash
- Loop through an array of strings in Bash?: https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash
- Redirect stderr and stdout in Bash: https://stackoverflow.com/questions/637827/redirect-stderr-and-stdout-in-bash[17][18][19]Cite error: Invalid
<ref>
tag;
refs with no name must have content
References
- ↑ Headers in shell scripts: thttps://ubuntuforums.org/showthread.php?t=1892589
- ↑ Tab separated values in awk: https://stackoverflow.com/questions/5374239/tab-separated-values-in-awk
- ↑ wikipedia: nohup
- ↑ Bash Guide for Beginners -- Chapter 12. Catching signals: https://linux.die.net/Bash-Beginners-Guide/sect_12_02.html
- ↑ Can a shell script set environment variables of the calling shell?: https://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell
- ↑ Shell scripting example.sh with most common use-cases: https://github.com/redshark1802/Shell--Scripting-Cheatsheet/blob/master/basics
- ↑ Top 10 Best Cheat Sheets and Tutorials for Linux / UNIX Commands: http://www.cyberciti.biz/tips/linux-unix-commands-cheat-sheets.html
- ↑ Bash Cheat Sheet: http://www.johnstowers.co.nz/blog/pages/bash-cheat-sheet.html
- ↑ Original cheat sheet: http://www.cism.ucl.ac.be/Services/Formations/cheatsheet.pdf
- ↑ How to Pass Arguments to Shell Script: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html
- ↑ How to pass command-line parameter to shell script?: https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-command-line-parameter-to-shell-script-254396/
- ↑ Shell Scripts with Multiple Arguments: http://www.linux-mag.com/id/844/
- ↑ if else bash script: https://unix.stackexchange.com/questions/239852/if-else-bash-script
- ↑ Bash -- run command before a script exits?: https://stackoverflow.com/questions/2129923/bash-run-command-before-a-script-exits
- ↑ Use the Bash trap Statement to Clean Up Temporary Files: http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
- ↑ How "Exit Traps" Can Make Your Bash Scripts Way More Robust And Reliable: http://redsymbol.net/articles/bash-exit-traps/
- ↑ How to redirect standard error in bash: https://www.cyberciti.biz/faq/how-to-redirect-standard-error-in-bash/
- ↑ BASH Shell -- How To Redirect stderr To stdout (redirect stderr to a File ): https://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/
- ↑ Input/Output Redirection in the Shell: https://thoughtbot.com/blog/input-output-redirection-in-the-shell
See Also
MySQL | FTP | Cloud Computing | Batch Script | TCL