Need to know before starting scripting

Scripts
•Contain a series of commands
•An interpreter executes commands in the script
•Anything you can type at the command line, you can put in a script
•Great for automating tasks

Basic Syntax
•#! /bin/bash
# Commands
•Shebang / HashBang • #! /bin/bash • Informs Linux which command line interpreter to use for the script; in this example, the Bourne Again Shell

Shell
Global Shell Configuration Files
•/etc/profile
•/etc/profile.d
•/etc/bashrc
•/etc/bash.bashrc
•/etc/skel • Contents of this directory are copied to new users directories when a new user is created User Shell Configuration Files
•~/.bash_login • Executes whatever commands are within the file ( ~/.bash_login ) when a user logs in

•~/.profile • User-specific bash configuration file
•~/.bash_profile • User-specific bash configuration file
•~/.bashrc • User-specific bash configuration file; executes whatever commands are within the file (~/.bash_login) when a user logs in.
•~/.bash_logout • Executes whatever commands are within the file ( ~/.bash_logout ) when a user logs out

Shell Variables
•set • Shows shell variables for the current instance of the running shell
•Set your own shell variables • EXAMPLE=VAR ; echo $EXAMPLE
» » Creates the shell variable EXAMPLE and sets the value to VAR, then prints the variable’s value
• Remove shell variables • unset EXAMPLE ; echo $EXAMPLE
» » Removes the shell variable EXAMPLE; echo will show no display since $EXAMPLE is no longer set to any value

Environment Variables
•env • Shows all environment variables
•env | grep EXAMPLE • Prints current environment variables and then greps the result for the term EXAMPLE
•export EXAMPLE=VAR • Exports shell variable EXAMPLE to the environment variables
•EXAMPLE=VAR ; export EXAMPLE • Export a previously-defined shell variable to the environment variables
•After you log off, the environment variables you set will restore to default; to permanently set an environment variable, you must either edit the user configuration files or global configuration files for bash
•Add to .bashrc (for user):
» » ABC=”123″; export ABC
•Add to /etc/.bash.bashrc (for system):
» » ABC=”123″; export ABC

Common Environment Variables
•DISPLAY • X display name
•EDITOR • Name of default text editor
•HISTCONTROL • History command control options
•HOME • Path to home directory
•HOSTNAME • Current hostname
•LD_LIBRARY_PATH • Directories to look for when searching for shared libraries
•MAIL • Holds the location of the user mail spools
•PATH • Executable search path
•PS1 • Current shell prompt
•PWD • Path to current working directory
•SHELL • Path to login shell
•TERM • Login terminal type
•USER / USERNAME • Current user’s username
•VISUAL • Name of visual editor

Changing the Shell Prompt
•Basic syntax • PS1=’\[“\] ‘
•Prompt variables:
» » \h • hostname
» » \w • current working directory
» » \u • username
» » \@ • 12 hour am/pm date
» » \t • 24 hour hh:mm:ss
» » \T • 12 hour hh:mm:ss
» » \j • Number of jobs running on the shell
» » \d • Date (day of week, month, day of month)
» » \H • Full hostname (hostname.domain.com)
» » \n • New line

•Example • PS1='[`pwd`]$ ‘
» » Makes the shell prompt the path to current directory followed by the $ sign

File Tests
•-a • True if exists; may cause conflicts
•-e • True if exists
•-f • True if exists and is a regular file
•-d • True if exists and is a directory
•-c • True if exists and is a character special file
•-b • True if exists and is a block special file
•-p • True if exists and is a named pipe (FIFO)
•-S • True if is a socket file
•-L • True if exists and is a symbolic link
•-h • True if exists and is a symbolic link
•-g • True if exists and has sgid bit set
•-u • True if exists and has suid bit set
•-r • True if exists and is readable
•-w • True if exists and is writable
•-x • True if exists and is executable
•-s • True if exists and has size bigger than 0
•-t • True if file descriptor is open and refers to a terminal
• -nt • True, if is newer than
• -ot • True if is older than
• -ef • True if and refer to the same device and inode numbers

String Tests
•-z • True if is empty
•-n • True if is not empty; this is the default operation
• = • True if the strings are equal
• != • True if the strings are not equal
• < • True if sorts before lexicographically; remember to escape •
• > • True if sorts after lexicographically; remember to escape •

Arithmetic Tests
• -eq • True if the integers are equal
• -ne • True if the integers are NOT equal
• -le • True if the first integer is less than or equal second one
• -ge • True if the first integer is greater than or equal second one
• -lt • True if the first integer is less than second one
• -gt • True if the first integer is greater than second one

Misc Syntax
• -a • True if and are true; -a may also be used as a file test
• -o • True if either or is true
•! • True if is false
•( ) • Group a test (for precedence); in normal shell-usage, parentheses must be escaped; use “\(” and “\)”
•-o • True if the shell option is set
•-v • True if the variable has been set; use var[n] for array elements
•-R • True if the variable has been set and is a nameref variable (since 4.3-alpha)

##################################################
If Statements
Basic Syntax
•if [ condition ];
then
else
fi
#commands to be run if true
#commands to be run if false

##################################################
Else If Syntax
•When using else if within an if statement, you want to use elif
•if [ condition ];
then
fi
#commands to be run if true
elif [ condition ];
then
#commands to be run if true
else
#commands to be run if false

If Statement with Multiple Conditions
•if [ condition ] OPERATOR [ condition ];
if [ condition ] || [ condition ];
if [ $g == 1 && $c == 123 ] || [ $g == 2 && $c == 456 ];

•if [[ ( Condition ) OPERATOR ( Condition ) ]];
if [[ ( Condition ) || ( Condition ) ]];
if [[ ( $g == 1 && $c == 123 ) || ( $g == 2 && $c == 456 ) ]];

##################################################
Case Statements
•Case statements are used to check the value of a parameter and execute code depending on the value
•This is similar to the switch statement in other languages with some slight differences:
» » Instead of the word switch , use the word case
» » Where you would use case , instead list the pattern followed by a closing parenthesis
» » To break the command chain, use ;;
Basic Syntax
•case “$VAR” in
pattern_1 )
# Commands to be executed
;;
pattern_2 )
# Commands to be executed
;;
* )
# Default
;;
esac
##################################################
While Loop
Basic Syntax
•while [ condition ] do
#command(s)
#increment
• Example:
» » x=1
while [ $x -le 5 ]
do
echo “Welcome $x times”
x=$(( $x + 1 ))
done
» » The above loop will run a command while x is less than or equal to 5
» » The last line adds 1 to x on each iteration
##################################################
For Loop
Basic Syntax
•for arg in [list]
do
done
#command(s)
•Any variable name can be used in place of arg
•Brace-expanded {1..5} items can be used in place of [list]
•During each pass through the loop, arg takes on the value of each successive variable in the list
•Example:
» » for COLOR in red green blue do
done
echo “COLOR: $COLOR”
» » # Output:
# Color: red
# Color: green
# Color: blue
##################################################

Leave a comment