Shell Script

From BC$ MobileTV Wiki
(Redirected from Shell)
Jump to: navigation, search

A Shell Script is a system-level control program which enables certain operations to be automated.


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

[1]


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

[2]


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

[4]



Environment Variables

[5]


Resources


Tutorials


External Links

refs with no name must have content


References

  1. Headers in shell scripts: thttps://ubuntuforums.org/showthread.php?t=1892589
  2. Tab separated values in awk: https://stackoverflow.com/questions/5374239/tab-separated-values-in-awk
  3. wikipedia: nohup
  4. Bash Guide for Beginners -- Chapter 12. Catching signals: https://linux.die.net/Bash-Beginners-Guide/sect_12_02.html
  5. 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
  6. Shell scripting example.sh with most common use-cases: https://github.com/redshark1802/Shell--Scripting-Cheatsheet/blob/master/basics
  7. Top 10 Best Cheat Sheets and Tutorials for Linux / UNIX Commands: http://www.cyberciti.biz/tips/linux-unix-commands-cheat-sheets.html
  8. Bash Cheat Sheet: http://www.johnstowers.co.nz/blog/pages/bash-cheat-sheet.html
  9. Original cheat sheet: http://www.cism.ucl.ac.be/Services/Formations/cheatsheet.pdf
  10. How to Pass Arguments to Shell Script: http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html
  11. 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/
  12. Shell Scripts with Multiple Arguments: http://www.linux-mag.com/id/844/
  13. if else bash script: https://unix.stackexchange.com/questions/239852/if-else-bash-script
  14. Bash -- run command before a script exits?: https://stackoverflow.com/questions/2129923/bash-run-command-before-a-script-exits
  15. Use the Bash trap Statement to Clean Up Temporary Files: http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
  16. How "Exit Traps" Can Make Your Bash Scripts Way More Robust And Reliable: http://redsymbol.net/articles/bash-exit-traps/
  17. How to redirect standard error in bash: https://www.cyberciti.biz/faq/how-to-redirect-standard-error-in-bash/
  18. BASH Shell -- How To Redirect stderr To stdout (redirect stderr to a File ): https://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/
  19. 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