icon

Shell Scripting

Last modified: 2023-11-03

For Loop

#!/bin/bash
for i in {1..5}
do
    echo "Hello $i"
done

4 Digits Loop

#!/bin/bash
for i in {0000..9999}
do
    echo $i
done

Read Text Line by Line

read-text.sh

#!/bin/bash
while read line
do
    echo $line
done < example.txt

Retrieve Arguments

Getopts is useful.
Create "my_opt.sh".

#!/bin/bash

getopts c cmd
echo $cmd

Run. This output is the current username.

./my_opt.sh -cwhoami
  • While Loop and Retrieving Arguments

    Create “my_while_opt.sh”.
    
    ```sh
    while getopts ab: flag
    do
        case "${flag}" in
            a) command1=${OPTARG};;
            b) command2=${OPTARG};;
        esac
    done
    
    cmd1=$($command1)
    echo $cmd1
    cmd2=$($command2)
    echo $cmd2
    

    Run. This output is the current username and current time.

    ./my_while_opt.sh -awhoami -btime
    # kali
    # real  1111.11s ...