Shell script best practices:
- Variable name should not have a dashes, use underscore instead.
- define meaningful variable name and name of the file as well.
- Variable should be encapsulated in { }. Change variable to ${file_name}_bkp
- Design your script to be re-usable
- Script should not require to be edited before running
- use command line argument to pass inputs
- try to avoid using backtick in script instead of that use $( )
- always start with a shebang in your script
- always return appropriate exit codes in your script like if something is fail then use exit 1.
- For more best practices refer this doc
What is Bash(Bourne-Again SHell)
Bash is a command language interpreter. It is widely available on various operating systems and is a default command interpreter on most GNU/Linux systems.
$ which bash
/bin/bash
$ cat hello.sh
#!/bin/bash
echo "Hello World"
$ cat test.sh
echo "Hello World"
$ cat test2.sh
#!/bin/bash
# This is comment
user=$(whoami)
input=/home/$user
output=/tmp/${user}_home_$(date +%Y-%m-%d_%H%M%S).tar.gz
echo "Backup of $input completed! "
- If you want to define your script's interpreter as Bash, try to locate the path of bash and then add the path by add #! prefix.
- Another way to call bash interpreter explictly is to run script with bash i.e bash test.sh
- ${parameter} is called parameter expansion since $user is followed by character which is not part of variable
#!/bin/bash
echo $1 $2 $4 ## positional parameters
echo $# ## the total number of supplied arguments
echo $* ## to print all arguments.
Syntax or For loop and While loop:
#!/bin/bash
echo $1 $2 $4 ## positional parameters
echo $# ## the total number of supplied arguments
echo $* ## to print all arguments.
echo $1 $2 $4 ## positional parameters
echo $# ## the total number of supplied arguments
echo $* ## to print all arguments.
Syntax or For loop and While loop:
$ cat for-loop.sh
#!/bin/bash
for i in 1 2 3; do
echo $i
done
$ cat while-loop.sh
#!/bin/bash
counter=0
while [ $counter -lt 3 ]; do
let counter+=1
echo $counter
done
$ cat for-loop.sh
#!/bin/bash
for i in 1 2 3; do
echo $i
done
$ cat while-loop.sh
#!/bin/bash
counter=0
while [ $counter -lt 3 ]; do
let counter+=1
echo $counter
done
#!/bin/bash
for i in 1 2 3; do
echo $i
done
$ cat while-loop.sh
#!/bin/bash
counter=0
while [ $counter -lt 3 ]; do
let counter+=1
echo $counter
done
Read input in shell script from user :
read -p "Enter you name: " name
Note : The value will be store in name varialbe
Note : The value will be store in name varialbe
Arithmetic operations in shell:
##using expression
exp 6 + 3
Note : please mind the space while using exp, and in shell * is reserved keyworkd so while multiplying you need to escape it using backslash
for operation you could use double parenthesis
echo $(( A + B ))
echo $(( ++A ))
for floating point answer you need to use bc
A = 10
B = 3
echo $A / $B | bc -l
for ((i = 1; i <= MAX_NO; i++)); do echo $i; done;
exp 6 + 3
Note : please mind the space while using exp, and in shell * is reserved keyworkd so while multiplying you need to escape it using backslash
for operation you could use double parenthesis
echo $(( A + B ))
echo $(( ++A ))
for floating point answer you need to use bc
A = 10
B = 3
echo $A / $B | bc -l
for ((i = 1; i <= MAX_NO; i++)); do echo $i; done;
Conditional logic in shell :
#########String comparison
if [ $rocket_status = "failed" ]
string --> =,!=, >, <
#########Arithmetic comparison
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ]; then echo "enter b/w 5 & 9" fi
number --> -eq, -ge, -gt, -le, -lt, -ne
if [ $rocket_status = "failed" ]
string --> =,!=, >, <
#########Arithmetic comparison
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ]; then echo "enter b/w 5 & 9" fi
number --> -eq, -ge, -gt, -le, -lt, -ne
Double bracket in shell(only in bash) :
[[ STRING1 = STRING2 ]]
[[ supports some extra features like following, this is been found in bash extension shell or latest shell
[[ "abcd" = *bc* ]]
if abcd contains bc true | no "" given since it is pattern
[[ "abc" = ab[cd] ]]
if 3rd character of abc is c or d true
[[ "abc" > "bcd" ]]
if abc comes after bcd when sorted in alphabetical lexographical order
------------------------------------------------------------------------------
conditional operations:
[ COND1 ] && [ COND2 ]
[ COND1 ] || [ COND2 ]
enhanced version:
[[ COND1 && COND2 ]]
[[ COND1 || COND2 ]]
Example:
if [ $first -eq 0 ] && [ $second -eq 0 ]
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ]; then echo "hello"; fi;
[[ supports some extra features like following, this is been found in bash extension shell or latest shell
[[ "abcd" = *bc* ]]
if abcd contains bc true | no "" given since it is pattern
[[ "abc" = ab[cd] ]]
if 3rd character of abc is c or d true
[[ "abc" > "bcd" ]]
if abc comes after bcd when sorted in alphabetical lexographical order
------------------------------------------------------------------------------
conditional operations:
[ COND1 ] && [ COND2 ]
[ COND1 ] || [ COND2 ]
enhanced version:
[[ COND1 && COND2 ]]
[[ COND1 || COND2 ]]
Example:
if [ $first -eq 0 ] && [ $second -eq 0 ]
if ! [ $MAX_NO -ge 5 -a $MAX_NO -le 9 ]; then echo "hello"; fi;
[ -e FILE ] if file exists
[ -d FILE ] if file exists and is a directory
[ -s FILE ] if file exists and has size greater than o
[ -x FILE ] if file is executable
[ -w FILE ] if file is writable
Use -z $1 to check for command line argument.
[ -d FILE ] if file exists and is a directory
[ -s FILE ] if file exists and has size greater than o
[ -x FILE ] if file is executable
[ -w FILE ] if file is writable
Use -z $1 to check for command line argument.
Some good example of above :
for file in $(ls images)
do
if [[ $file = *.jpeg ]]
then
new_name=$(echo $file| sed 's/jpeg/jpg/g')
mv images/$file images/$new_name
fi
done
do
if [[ $file = *.jpeg ]]
then
new_name=$(echo $file| sed 's/jpeg/jpg/g')
mv images/$file images/$new_name
fi
done
Loop over list of pdfs file name from batch3.txt and see if that exists in CSV file :
#!/bin/bash
i=1
while read p; do
echo "$p"
file="s3://test-bucket-images/v2/batch3/$p".
echo $file
echo $i
i=$((i+1))
line=`cat textract.csv | grep "$file" | wc -l`
echo $line
if [ $line -eq 0 ];
then
echo $file > missing.txt
fi
done <batch3.txt
i=1
while read p; do
echo "$p"
file="s3://test-bucket-images/v2/batch3/$p".
echo $file
echo $i
i=$((i+1))
line=`cat textract.csv | grep "$file" | wc -l`
echo $line
if [ $line -eq 0 ];
then
echo $file > missing.txt
fi
done <batch3.txt
Reference:
0 Comments