Simple Bash Scripts
- Example Scripts are found in the /home/cccc/src directory. We will copy them as needed.
- examples that run your machine : /etc/rcS.d/*.sh
- variables See examples in the variables directory.
- $ indicates a variable. See hello.sh
- $#, $?, $0, $*, $@ are built into the bash. # of arguments, ? last exit value, 0 name of shell program, * all arguments, @ all in quotes.
- Environment variables. (Note the caps)
- run env to see them.
- $LOGNAME
- $HOME
- $USER
- $PWD
- set them in bash.profile, bash.rc or your script.
- Local variables are seen only in a function and do not affect variables by the same name outside the function. See local.sh
- Redirection and Pipes
- ">" is output redirection to a file.
- "<", input redirection is useful for commands that don't take a file.
- The standard file descriptors, stdin, stdout, stderr are referenced like pointers for redirection.
- "ls -l > ls_l.txt" puts list into a file.
- "grep da * 2> grep-errors.txt" puts errors messages into a file.
- "grep * 2>&1>out.txt" gets both.
- "|" strings together files. "find | grep avi" is a lazy way to find movie files.
- ls -l | grep "\.txt$" is a painful replacement for ls *.txt, but useful as above.
- evaluating stuff
- test
- conditional stagements and expressions, integers like fortran, strings like C, file operators are nice. if fi just like C. see example /condition/if.sh
- Loops
- Functions
- automating simple tasks