Shell Scripting Basics/shaare/wcZcmg
First Line of Script
#!/bin/bash
→ defines the shell interpreter
Comments
- Use
#
for commenting
Common Elements
- Commands:
echo
,cp
, etc. - Statements:
if
,while
,for
+x
→ make the script executable- Use absolute path to run:
/home/userdir/myscript.bash
Variable Definition
a=hello
b=mytext
- Usage:
echo "text1 $a"
echo "text2 $b"
- Example:
a=$(hostname)
echo $a # => myfirstlinux
Input / Output Variable
read myinputvariable
echo "name $myinputvariable"
If Else Statement
if [[ $count -eq 100 ]]; then
echo "count is 100"
else
echo "no"
fi
File Existence Check
if [[ -e /file.txt ]]; then
echo "file exists"
fi