Shell Scriptiong Basics

Telling Script the location of bash shell binary.

#! /bin/bash

for zshell

#! /bin/zsh

-v flag print every command before executing #! /bin/bash -v

-x flag print the command but also the value of variable #! /bin/bash -x

grep h6 shipments.csv > reports/h6.csv

Displaying output to console echo Report Created

Creating Variables,

variable names are case sensitive, use lower case names

dont use space on either side of variable

directory=reports

variables can contain file names

filename=shipments.csv

valuses containing spaces: use quotes filenames=“notes.txt picture.jpg movie.mov”

using created variable to use a variable we need to put the dollar sign($) before variable, like $directory

Using user input as variable in script directory=reports mkdir -p $dirctory

for first argument passed to the script we use that arugument with special variable called $1, to use second argument passed by user wehen calling the script we use variable $2 and so on.

grep $1 shipments.csv > $directory/$1.csv echo Wrote report $directory/$1.csv

Examples

container=$1 directory=$2

mkdir -p $directory grep $container shipments.csv > $directory/$container.csv echo Wrote report $directory/$container.csv

Use of Qoutes in writing scriptis Qoutes remove any special meaning of spaces or other letters.

container="$1" directory="$2"

mkdir -p “$directory” grep “$container” shipments.csv > “$directory/$container.csv” echo “Wrote report $directory/$container.csv”

Checking scripts for mistakes

https://www.shellcheck.net/

shellcheck script-name.sh

End of options feature

end of options feature -- which will be used to protect shell if the user pass string arguments as options to script.

container="$1" directory="$2"

mkdir -p – “$directory” grep – “$container” shipments.csv > “$directory/$container.csv” echo “Wrote report $directory/$container.csv”

printf

printf is more powerfull and secure than echo

printf “I am %s and my shell is %s\n” $USER $SHELL

use of braces in script

container="$1" directory="$2"

mkdir -p – “$directory” grep – “$container” shipments.csv > “$directory/${container}_reports.csv” echo “Wrote report $directory/${container}_reports.csv”

Changing the input_file to variable.

“$input_file” variable will get its value from .bashrc file where it is defined.

container="$1" directory="$2"

mkdir -p – “$directory” grep – “$container” “$input_file” > “$directory/${container}_reports.csv” echo “Wrote report $directory/${container}_reports.csv”

export PATH="$HOME/bin/:$PATH" export input_file="$HOME/shipments.csv"

calling script create_report.sh G2 ~/reports

Adding folders to path

open .bashrc file located at $HOME directory of user.

at the end of the file type

export PATH="$HOME/bin/:$PATH" export variable_name="$HOME/shipments.csv"

Conditionals in Scripting

if mkdir a; then echo "ok"; else echo "error"; fi

Return values of program linux programs exit with return code from 0-255, 0 means success, other values are error codes. Return value from script exit 0

Good habit exit scripts with correct value Always call exit with a value

Condition Expressions

Test for return value of command if touch /tmp/x; then …

perform test on strings, files, numbers

if [[ $str ]]; then

fi

Examples put spaces arround variable, [[ $str ]]# String not empty

putting spaces arround equal is important. [[ $str = "somthing" ]] # String wquals something, [[ -e $filename ]] #file exists [[ ! -e $filename ]] #file does not exists [[ -d $filename ]] #directory exists [[ -! $1 ]] #Argument empty

[[ -d $filename && $1 = "foo" ]] #directory exists and first argument is foo then do this [[ -d $filename || $1 = "foo" ]] #directory exists or first argument is foo then do this

Tip: Don’t use -a for and -o for OR. use modern && and || for and and OR.

Appending Text to file

https://linuxize.com/post/bash-append-to-file/

Replacing content in the file

#!/bin/bash

addr=$1
port=$2
user=$3

sed -i -e "s/\(address=\).*/\1$1/" \
-e "s/\(port=\).*/\1$2/" \
-e "s/\(username=\).*/\1$3/" xyz.cfg

changing simple value in a file

this line can be copied and used for changing single values in the scripts, like changing anonymous_enable=YES to anonymous_enable=NO.

sudo sed -i.bak 's/\b\(anonymous_enable=\).*\b/anonymous_enable=YES/g' /etc/vsftpd.conf

Commenting and uncommenting the line with bash script

https://linuxize.com/post/how-to-use-sed-to-find-and-replace-string-in-files/ https://linuxhint.com/replace_string_in_file_bash/

Executing Script

chmod u+x script.sh