Shell Scripting Flashcards
Executing a script
A. Writing it directly in bash interpreter
B. First write the script then execute
A.
1. Go to directory where the script should live
2. Create a file with .sh extension
3. Write script in that file using an editor (e.g. vi)
Indicate the file is a bash script with #!/bin/bash
4. Make script executable >chmod 755 [script name].sh
5. Run script > ./[script name].sh
B.
- Start script with : #!/bin/bash
- Apply executable permission to .sh file > chmod 755 [script name].sh
- Run script
Shell script with variable in script
#! /bin/bash MY_SHELL="eat" echo "I am ${MY_SHELL}ing my keyboard."
Output
I am eating my keyboard
Shell script with output of a command as variable
!# /bin/bash
SERVER_NAME = $(hostname) »_space;this is the command
echo “You use ${hostname}”
Output
You use Linux
Executing Python scripts via the interpreter
- Write python script
> #! /usr/bin/python
> print “THis is a python script” - Add execute permissions
>chmod 755 hi.py - Run script from present directory
./hi.py
This is a Python script.
File Operators
- d FILE True if file is a directory.
- e FILE True if file exists.
- f FILE True if file exists and is a regular file.
- r FILE True if file is readable by you.
- s FILE True if file exists and is not empty.
- w FILE True if the file is writable by you.
- x FILE True if the file is executable by you
String Operators
-z STRING True if string is empty.
-n STRING True if string is not empty.
STRING1 = STRING2
True if the strings are equal.
STRING1 != STRING2
True if the strings are not equal
Arithmetic operators
arg1 –eq arg2 True if arg1 is equal to arg2.
arg1 –ne arg2 True if arg1 is not equal to arg2.
arg1 –lt arg2 True if arg1 is less than arg2.
arg1 –le arg2 True if arg1 is less than or equal to arg2.
arg1 –gt arg2 True if arg1 is greater than arg2.
arg1 –ge arg2 True if arg1 is greater than or equal to arg2
IF Statement Syntax
if [ condition-is-true ] then command 1 command 2 command N fi
IF Statement script - Example
#!/bin/bash MY_SHELL="bash" if [ "$MY_SHELL" = "bash" ] then echo "You seem to like the bash shell." fi
Output:
You seem to like the bash shell.
IF/THEN/ELSE Syntax
if [ condition-is-true ] then command N else command N fi
IF/THEN/ELSE Script Example
#!/bin/bash MY_SHELL="csh" if [ "$MY_SHELL" = "bash" ] then echo "You seem to like the bash shell." else echo "You don't seem to like the bash shell." fi
IF / ELIF / ELSE Syntax
if [ condition-is-true ] then command N elif [ condition-is-true ] then command N else command N fi
IF / ELIF / ELSE Example
!/bin/bash
MY_SHELL=”csh”
if [ “$MY_SHELL” = “bash” ]
then
echo “You seem to like the bash shell.”
elif [ “$MY_SHELL” = “csh” ]
then
echo “You seem to like the csh shell.”
else
echo “You don’t seem to like the bash or csh shells.”
fi
FOR Loop Syntax
for VARIABLE_NAME in ITEM_1 ITEM_N do command 1 command 2 command N done
FOR Loop Script Example
#!/bin/bash for COLOR in red green blue do echo "COLOR: $COLOR" done
Output: COLOR: red COLOR: green COLOR: blue \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ #!/bin/bash COLORS="red green blue" for COLOR in $COLORS do echo "COLOR: $COLOR" done