# Shell Scripting - Loops & Command Line Arguments

These topics are covered in today's session by @[Pranav Jambare](@AutomateMaster)

### **Loops in Linux**

* Loops in shell scripting allow you to **execute a series of commands** repeatedly, based on a **condition** or a **list of items**.
    

### **Types of Loops**

**While Loop**

* The while loop repeatedly executes the commands inside the loop as long as the condition **remains true**.
    
* If the condition **becomes false**, the loop **stops executing** and moves to the next part of the script.
    

```bash
#Syntax:
while <command>;
do
    # Statement execute if command is true
done
```

```bash
#Example: (Create a sample.sh and edit the file with the given script)
#Printing numbers from 1 to 20
#!/bin/bash
A=1
while [ ${A} -le 20 ];
do
    echo "A = ${A}"
    A=`expr ${A} + 1` 
done

#After running script...
A = 1
A = 2
...
A = 19
A = 20
```

---

**Until Loop**

* The until loop is **similar** to the while loop, but it **continues executing** the commands inside the loop **until** the condition becomes **false**.
    
* Once the condition is **true**, the **loop stops executing**.
    

```bash
#Syntax:
until <command>;
do
    # Statement execute if command is false
done
```

```bash
#Example: (Create a sample.sh and edit the file with the given script)
#Printing numbers from 20 to 1
#!/bin/bash
A=20
while [ ${A} -ge 1 ];
do
    echo "A = ${A}"
    A=`expr ${A} - 1` 
done

#After running script...
A = 20
A = 19
...
A = 2
A = 1
```

---

**For Loop**

* The for loop **iterates** over **each item** in the list, and the **variable** takes the **value** of each item in each iteration.
    
* The commands inside the loop are executed for **each item in the list**.
    

```bash
#Syntax:
for var in word1 word2 .. wordn;
do
    # Statement executed till element end
done
```

```bash
#Example:
#Printing the names of students...
#!/bin/bash
A=1
for studName in Viraj Amogh Suyash;
do
    echo "The name of student ${A} is : ${studName}"
    A=`expr ${A} + 1`
done

#After running the script...
The name of student 1 is : Viraj
The name of student 2 is : Amogh
The name of student 3 is : Suyash
```

```bash
#Example:
#To print alphabets from A to Z
#!/bin/bash
for alpha in {A..Z};
do
    echo "${alpha}"
done

#After running the script...
A
B...
..
Y
Z
```

```bash
#Example:
#To print the names of students from a file
#!/bin/bash
for name in $(cat /home/viraj/namesFile);
do
    echo "The name is : ${name}"
done
```

---

### **Command Line Arguments**

* Command Line Arguments are **inputs** or **parameters** provided to a **command** or **script** when it is **executed** in the terminal.
    
* $1 $2 $3 --&gt; The **first** command-line argument is represented by **$1**, the **second** one by **$2**, and so on.
    
* $0 --&gt; Represents the **name** of the **script** itself.
    
* $@ --&gt; Passing arguments one-by-one
    
* $# --&gt; **Total number** of command line arguments provided
    
* $\* --&gt; Represents all the command-line arguments as a **single string**
    
* $$ --&gt; Represents the process ID (**PID**) of the currently running **shell** or **script**
    
* $? --&gt; Holds the status of the last executed command
    
* $! --&gt; Hold the process ID (**PID**) of the last executed command
    

```bash
#Example:
#!/bin/bash
for names in $1 $2;
do
    echo "Your name is : ${names}"
    echo "The name of script is : $0"
    echo "Passing args one-by-one : $@"
    echo "Total number of args provided : $#"
    echo "Args as a single string : $*"
    echo "PID of current script : $$"
    echo "Status of last executed cmd: $?"
    echo "PID of last executed cmd: $!"
done

#After running script...
Your name is : Viraj
The name of script is : sampleFile.sh
Passing args one-by-one : Viraj Amogh
Total number of args provided : 2
Args as a single string : Viraj Amogh
PID of current script : 4920
Status of last executed cmd : 0
PID of last executed cmd : --

Your name is : Amogh
The name of script is : sampleFile.sh
Passing args one-by-one : Viraj Amogh
Total number of args provided : 2
Args as a single string : Viraj Amogh
PID of current script : 4920
Status of last executed cmd : 0
PID of last executed cmd : --
```

---

### **Shell Scripting Question**

**Q.** Create 5 directories and take user input to read name of those directories. Now create two files in each directory and take user input to name those files. Now take user input to add some content in each file.

```bash
#Answer:
#!/bin/bash
for dirName in {1..5};
do 
    echo "Enter the name of directory : "
    read dir
    mkdir ${dir}
       
    for fileName in 1 2;
    do 
        echo "Enter the name of file : "
        read file
        
        touch /home/viraj/${dir}/${file}

        echo "Enter the content for file : "
        read content >> /home/viraj/${dir}/${file}
    done
done
```

---
