[go: up one dir, main page]

0% found this document useful (0 votes)
28 views23 pages

Linux Bash Scripting

This document provides a summary of common Linux Bash scripting commands and concepts. It describes various commands like vi, &&, ||, ^, $, -eq, and more. It also covers bash startup files, variables, arrays, conditions, loops, command line arguments, head and tail commands, and multiline strings. Overall, the document serves as a useful reference for Linux Bash scripting fundamentals.

Uploaded by

konlinuxadm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views23 pages

Linux Bash Scripting

This document provides a summary of common Linux Bash scripting commands and concepts. It describes various commands like vi, &&, ||, ^, $, -eq, and more. It also covers bash startup files, variables, arrays, conditions, loops, command line arguments, head and tail commands, and multiline strings. Overall, the document serves as a useful reference for Linux Bash scripting fundamentals.

Uploaded by

konlinuxadm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Linux Bash Scripting

Command Description Example


Vi vi Run the previous command in ls &
the background
&& Logical AND if [ "$foo" -ge "0" ] && [ "$foo" -
le "9"]
|| Logical OR if [ "$foo" -lt "0" ] || [ "$foo" -
gt "9" ]
^ Start of line grep "^foo"
$ End of line grep "foo$"
= String equality (cf. -eq) if [ "$foo" = "bar" ]
! Logical NOT if [ "$foo" != "bar" ]
$$ PID of current shell echo "my PID = $$"
$! PID of last background ls & echo "PID of ls = $!"
command
$? exit status of last command ls ; echo "ls returned code $?"
$0 Name of current command (as echo "I am $0"
called)
$1 Name of current command's echo "My first argument is $1"
first parameter
$9 Name of current command's echo "My ninth argument is $9"
ninth parameter
$@ All current command's echo "My arguments are $@"
parameters (preserving
whitespace and quoting)
$* All current command's echo "My arguments are $*"
parameters (not preserving
whitespace and quoting)
-eq Numeric Equality if [ "$foo" -eq "9" ]
-ne Numeric Inequality if [ "$foo" -ne "9" ]
-lt Less Than if [ "$foo" -lt "9" ]
-le Less Than or Equal if [ "$foo" -le "9" ]
-gt Greater Than if [ "$foo" -gt "9" ]
-ge Greater Than or Equal if [ "$foo" -ge "9" ]
-z String is zero length if [ -z "$foo" ]
-n String is not zero length if [ -n "$foo" ]
-nt Newer Than if [ "$file1" -nt "$file2" ]
-d Is a Directory if [ -d /bin ]
-f Is a File if [ -f /bin/ls ]
-r Is a readable file if [ -r /bin/ls ]
-w Is a writable file if [ -w /bin/ls ]
-x Is an executable file if [ -x /bin/ls ]
( ... ) Function definition function myfunc() { echo hello }

Startup files:
When ever a terminal was created (console or remote connection/session), bash executes few files at
Startup in Interactive login shell like /etc/profile or ~/.bash_profile, ~/.bash_login, and ~/.bashrc
(Interactive non-login shell)

Difference between ~/.bash_profile and ~/.bashrc files

~/.bash_profile is read and executed when bash is invoked as an interactive login shell (Login to system
using ssh)

~/.bashrc is executed for an interactive non-login shell (Invoking a new shell on an already logged-in
shell)

Use ~/.bash_profile to run commands that should run only once, such as customizing the $PATH
(environment variables)

Use ~/.bashrc file if you want commands that should run every time you launch a new shell. (aliases and
functions, custom prompts, history customizations etc.,)

~/.bash_profile is only used by BASH shell and ~/.profiles file is read by all shells (First it looks for
~/.bash_profile file, if it doesn’t exist then it will look for ~/.profile)

Variables:
There are two different ways to define a variables in shell: Environment variables and user-defined
variables

Environment variables are pre-defined variables which will be loaded by default when we establish a
connection/login to Machine which terminal will be created, and default variables will be loaded which
shows in UPPER case letters.

#env or #printenv
If you want to set own environment variables to be loaded when connections/login to machine, which
users no need to create these environment variables.

These variables are temporary, since ever session/terminal will have its own environment variables
loaded by default. If we need to add our own environment variables, we need to add in ~/.bashrc or
~/.profile file.

User defined variables will be created and exists

Arrays:
An array is a systematic arrangement of the same type of data. But in Shell script Array is a variable
which contains multiple values may be of same type or different type since by default in shell script
everything is treated as a string. An array is zero-based i.e., indexing start with 0.

Array is defined in three different ways: Indirect array, Explicit array, and Compound assignment

```

ARRAYNAME[INDEX]=value

```

```

declare -a ARRAYNAME

```

```

ARRAYNAME=(value1 value2 value3 .. valueN)

```

Print array values

#echo ${ARRAYNAME[INDEX/0/1/2]}

#echo ${ARRAYNAME[*]}

#echo ${ARRAYNAME[@]}

#echo ${#ARRAYNAME[*]}

#echo ${ARRAYNAME[@]:0/1/2/3}

#echo ${ARRAYNAME[@]:STARTINDEX:COUNTELEMENT}

Deleting an element in a ARRAY

#unset ARRAYNAME[INDEX]

#echo ${ARRAYNAME[@]:index:range}
#unset ARRAYNAME  to delete whole array

Conditions:
There are 5 conditional statements

1) If statement
2) If…else statement
3) If…elif...else...fi statement (else if ladder)
4) If…then…else…if…then…fi…fi (Nested if)
5) switch statement (case statement)

if statement
This statement block will process if specific condition is true

Syntax

```

If [ expression ]
then

statement

fi

or

If [ expression ]; then

statement

fi

```

Examples:
If…else statement
If the condition is not true, then else block will be executed

Syntax

```

If [ expression ]

then

statement1

else

statement2

fi

or

If [ expression ]; then

statement1

else

statement2

fi

```
Pattern match conditions, use single braces and see how it works. Then add additional brace to check
patterns

if…elif…else…fi statement (Else if ladder)


To check multiple conditions and execute the statement when that condition is true

Syntax

```

If [ expression1 ]

then

statement1

statement2

elif [ expression2 ]
then

statement3

statement4

else

statement5

fi

```

If…then…else…if…then…fi…fi (Nested if)


Nested if-else block can be used when, one condition is satisfies then it again checks another condition

```

If [ expression ]

then

statement1

statement2

else

If [ expression2 ]

statement3

statement4

fi

fi

```

Switch/Case Statement
It works as a switch statement like if a value match any of the pattern, then it will be executed

Syntax

```

case in

pattern1)

statement1;;

statement2;;
esac

```

Loops:
There are three types of loops that can created in bash scripting

1) For loop
2) While
3) Until loop

Conditions in Loops are break and continue

For loop:
It is used to execute set of commands/values for every item in the list.

```

for VARIABLE in 1 2 3 4 5 .. N

do

command

command

command

done

or

for VARIABLE in $(command)  Command substitution

do
statement1

statement2

statement3

done

```

Conditional For loop


Infinite For loop

For loop with break statement


For loop with continue statement

Logical script For loop

Command line arguments:


$0, $1, $2 … $9  positional arguments and $* will be treated as single quote

$@ will give the arguments passed and $# will give no of arguments passed

While loop:
It allows us to execute a set of commands repeatedly until some condition occurs.

```

while command/condition

do

statements

done

```
Run infinity loop using while

#!/bin/bash

COUNTER=0

while [ $COUNTER -lt 10 ]; do

echo The counter is $COUNTER

let COUNTER=COUNTER+1 #COUNTER=`expr $COUNTER +1`

done

Infinity while loop:

#!/bin/bash

while :

do

echo "Please Ctrl + C to stop the loop"

done

root@ubuntuserverdemo:~/scripting# cat while_loop.sh

#!/bin/bash

while true

do

echo "Please Ctrl + C to stop the loop"


done

While loop example with break statement:


While loop example with continue statement:
C-style while loop in BASH

#!/bin/bash

i=0

while ((i<=12))

do

echo $i

((i++))

done

Select to make simple menus:


Difference between ${VARIABLE:-default}  assigned value, or default if it's missing and
${VARIABLE:=default}  assign default to VARIABLE at the same time
Until loop:
The while loop is perfect for a situation where you need to execute a set of commands while
some condition is true. Sometimes you need to execute a set of commands until a condition is
true.
```
until command/condition
do
statements
done
```

When something goes wrong and to debug, we can use -x with the shebang of first line of the script:

#!/bin/bash -x

Head and Tail commands:

Head:

By default, head will print only first 10 lines, even through the file or input data has more lines
#head filename

#head -n 20 filename or head -n+20 filename  This will print specified number of lines -n needs digit
instead if default 10 lines

#head -n -10 filename or head -n-10 filename  This will remove last 10 lines of a file

#head -n 4 file1 file2  Multiple file will display starting 4 lines

Tail:

By default, tail command will display last 10 lines of a file or a input data

#tail filename  By default last 10 lines will be printed

#tail -n 8 filename  Specified number of last lines will be printed and tail -n-2 filename  it will print
only last two lines

#tail -n+3 or tail -n +3 filename  it will remove starting 3 -1 lines

#tail -f filename  it will print live data of a file

Head and tail commands together

For example, I want to print 5th line to 10th line:

#tail -n +5 filename | head -n 5

#head -n 5 filename | tail -n 5

Multiline strings:

```

Cat <<EOF > demo-data.sh

#!/bin/bash

echo “Updated input from command line EOF”

EOF

Now cat the file and see the lines added

or

cat <<EOF | grep sudheer | tee /tmp/data

visualpath

testline

third line

sudheer
EOF

```

AWK command:

Syntax: awk options ‘criteria { action }’ filename

cat /etc/passwd | awk -F: '{print $3}'  It will print third column in /etc/passwd file (UID)

cat /etc/passwd | awk -F: '/testuser/ {print $3}'  it will print UID of user “testuser”

cat /etc/passwd | awk -F: '/testuser/ {print $1, $3}'  it will print username and UID for testuser

cat /etc/passwd | awk -F: '/testuser/ {print NR,$1, $3}'  It will above info along with line number with
matching once

cat /etc/passwd | awk -F: '/testuser/ {print NR,$1, $3, $NF}'  It will print last column as well along with
above info

cat /etc/passwd | awk -F: '{print NR "-" $1}'


cat /etc/passwd | awk 'END { print NR }'  prints no of lines in input file passed
cat /etc/passwd | awk '{ if (length($0) > max) max = length($0) } END { print max }'  prints
length of the longest line present in the file
cat /etc/passwd | awk 'length($0) > 10'  It will print lines more than 10 characters

Sed Command:
sed 's/sudheerdemo/visualpathdemo/' passwd_copy  It will replace/substitute the word
sudheerdemo with visualpathdemo in each line of first occurrence
To modify each occurrence in line, we need to pass /g
sed 's/sudheerdemo/visualpathdemo/g' passwd_copy
sed 's/sudheerdemo/visualpathdemo/3g' passwd_copy  modifies from nth occurrence till
end
Note: above commands will not actually modify, it will print the results on the terminal output
how it has changed. We can redirect and save the changes in another file if needed
sed '38s/sudheerdemo/visualpathdemo/g' passwd_copy  It will modify the changes on
specified line number  38th line here
sed -n 's/sudheerdemo/visualpathdemo/gp' passwd_copy  it will print only replaced lines
sed 'nd' passwd_copy (Example: delete 38th line in a file #sed '38d' passwd_copy)  It will
delete a line with nth value specified
sed '/visualpath/d' passwd_copy  It will delete a line with matched string
sed '/^$/d' passwd_copy  Deletes empty lines in a file
sed -i '/^#/d;/^$/d' passwd_copy  It deletes empty lines and # commented lines in single task
sed -n '2,5p' passwd_copy  print lines specified in the range (2 to 5)
cat passwd_copy | sed -n '/sudheerdemo/ s/\/bin\/sh/\/bin\/bash/p'  This command will
search for line with sudheerdemo and in that line it will replace “/bin/sh” to /bin/bash
CUT command:
Syntax:
#cut -d “delimiter” -f “field_number” filename or any other input
Programming

You might also like