From Network Security Wiki
(Redirected from Scripts)
Jump to navigation Jump to search



  • POSIX stands for Portable Operating System Interface, and is an IEEE standard designed to facilitate application portability.
  • POSIX is an attempt by a consortium of vendors to create a single standard version of UNIX.
  • If they are successful, it will make it easier to port applications between hardware platforms.
  • sh (or the Shell Command Language) is a programming language described by the POSIX standard.
  • It has many implementations (ksh88, dash, ...). bash can also be considered an implementation of sh (see below).
  • Because sh is a specification, not an implementation, /bin/sh is a symlink (or a hard link) to an actual implementation on most POSIX systems.
  • bash started as an sh-compatible implementation (although it predates the POSIX standard by a few years), but as time passed it has acquired many extensions.
  • Many of these extensions may change the behavior of valid POSIX shell scripts, so by itself bash is not a valid POSIX shell.
  • Rather, it is a dialect of the POSIX shell language.
  • bash supports a --posix switch, which makes it more POSIX-compliant.
  • It also tries to mimic POSIX if invoked as sh.


  • Declaring
$1, $2, $3    are the positional parameters.
"$@"          is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$*"          is the IFS expansion of all positional parameters, $1 $2 $3 ....
$#            is the number of positional parameters.
$-            current options set for the shell.
$$            pid of the current shell (not subshell).
$_            most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$IFS          is the (input) field separator.
$?            is the most recent foreground pipeline exit status.
$!            is the PID of the most recent background command.
$0            is the name of the shell or shell script.
  • Using Variables in Bash:
tree $dir | wc -l >> $path
  • Correct way to declare variable
Variable="Some string"
  • Wrong way to declare Varibales:
Variable = "Some string"  ==>  Bash will decide that Variable is a command it must execute
Variable= 'Some string'   ==> Bash will decide that 'Some string' is a command it must execute
  • Print first 10 characters:
echo ${text:0:10}

Builtin variables

echo "Last program's return value: $?"
echo "Script's PID: $$"
echo "Number of arguments passed to script: $#"
echo "All arguments passed to script: $@"
echo "Script's arguments separated into different variables: $1 $2..."


  • Declare an array with 6 elements
array0=(one two three four five six)
  • Print first element
echo $array0 # => "one"
  • Print first element
echo ${array0[0]} # => "one"
  • Print all elements
echo ${array0[@]} # => "one two three four five six"
  • Print number of elements
echo ${#array0[@]} # => "6"
  • Print number of characters in third element
echo ${#array0[2]} # => "5"
  • Print 2 elements starting from forth
echo ${array0[@]:3:2} # => "four five"
  • Print all elements. Each of them on new line.
 for i in "${array0[@]}"; do
    echo "$i"

Brace Expansion

echo {1..10}
echo {a..z}

Builtin variables

Both commands are quivalent

echo "I'm in $(pwd)"                  # execs `pwd` and interpolates output
echo "I'm in $PWD"                    # interpolates the built in `$PWD` variable

Reading Input

echo "What's your name?"
read Name                             # Note we didn't declare a variable
echo Hello, $Name!


echo $(( 10 + 5 ))

Read Files

Contents=$(cat file.txt)
echo "START OF FILE\n$Contents\nEND OF FILE"

Use subshells to work across directories

(echo "First, I'm here: $PWD") && (cd someDir; echo "Then, I'm here: $PWD")
pwd                  # still in first directory

Case Statements

case "$Variable" in
    #List patterns for the conditions you want to meet
    0) echo "There is a zero.";;
    1) echo "There is a one.";;
    *) echo "It is not null.";;


  • General Syntax:
for Variable in {1..3}
    echo "$Variable"
  • Traditional for loop way:
for ((a=1; a <= 3; a++))
    echo $a
  • Act on files:
for i in file1 file2
    cat "$i"
  • Output from a command
for i in $(ls)
    cat "$i"
  • While loop:
while [ true ]
    echo "This is loop..."


  • Declaration:
function foo ()
    echo "Arguments work just like script arguments: $@"
    echo "And: $1 $2..."
    echo "This is a function"
    return 0
foo arg1 arg2
  • Another way:
bar ()
    echo "Another way to declare functions!"
    return 0


Trap executes a command whenever script receives a signal (here any of the three listed signals):


Sudo is used to run commands as the superuser:

NAME2=$(sudo whoami)
echo "Was $NAME1, then became more powerful $NAME2"

Bash Scripts

One Liners

  • Script to find file extensions
find $dir -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
  • Convert files with name "webp" file to Png:
find . -name "*.webp" -exec dwebp {} -o {}.png \;
find . -name "*.webp" -delete
  • Create Multiple Directories with single command:
mkdir ep{1..16}
mkdir -p sa{0..10}/{1,2,3,4}
mkdir -p sa{0..10}/{1..4}
  • Download multiple files at once:
wget -i list.txt
  • Saving text use echo to continue on same line:
echo -n "Count: " >> files.txt
find . | wc -l >> files.txt
  • Word count for directories
IFS=$'\n'; for i in `find . -type d`; do echo $i; ls $i|wc -l; done; unset IFS
  • Find Lengths of multiple video files:
IFS=$'\n'; for i in `find . -name "*.mp4"`; do mediainfo $i | grep "Duration" | awk 'NR == 1 {print i $3$4,$5$6 $7$8}'; done; unset IFS

Small Scripts

Extract multiple tar files to sub-directories
for i in `find /home/test/ -type f -name '*.tar.gz'`; do echo $i; j=$(echo $i | cut -d '.' -f1); mkdir $j; tar xvzf $i -C $j; done
Tar Multiple files at once
for i in `find $(pwd) -type f`; do echo $i; j=$(echo $i | cut -d '.' -f1); echo $j; tar cvzf $"$j.tar.gz" $i; done
Checking Internet connectivity

if ping -c 1 -W 2 > /dev/null; then
 echo "Up"
 echo "Down"
Mplayer Play Live Gurbani from 8 to 10AM
 trickle -d 20 -u 15 mplayer mms:// &
 sleep 2h
 kill $!

Cron Entry:

0 8 * * * ./
Download Newspaper front page

 cd ~/Desktop
 D=$(date +"%Y%m%d")
 if [ -f $FILE ];
    wget ""$D"/"$D"_1_1.jpg" && notify-send -i /data/Softwares/Wallpapers/icons/ajit.png -t 50 "Ajit Jalandhar" "is saved at your desktop"

Cron Entry to download daily:

0 7-11 * * * ./
Eye strain preventer

while true; do sleep 1200; sm --foreground=white --background=black Look at an Object 20 feet away for 20 Seconds & sleep 20s && killall sm; done

Add following line to startup programs:

/home/aman/Scripts/ >/dev/null 2>&1
Extension Doctor
   Ambox notice.png     This script will overwrite any existing file.
for f in *.{jpg,JPG,png,PNG,jpeg,JPEG,webp}; do 
    type=$( file "$f" | grep -oP '\w+(?= image)' )
    case $type in  
        PNG)  newext=png ;; 
        JPEG) newext=jpg ;;
        P) newext=webp ;;
        *)    echo "??? what is this: $f"; continue ;; 
    ext=${f##*.}   # remove everything up to and including the last dot
    if [[ $ext != $newext ]]; then
        # remove "echo" if it's working correctly
        echo mv "$f" "${f%.*}.$newext"

Big Scripts

Directory Stats
#Script to export file/directory stats to a file.

# Path Variables:

# Script starts here:
echo "###########################">> $file

# Date of execution:
date >> $file && echo " ">> $file

# File Stats:
echo -n "No of Files: ">> $file
find $dir -type f | wc -l >> $file && echo " ">> $file

echo -n "No of Directories: ">> $file
find $dir -type d | wc -l >> $file && echo " ">> $file

# Directory size:
echo "Sub directory size: ">> $file
du -h --max-depth=1 $dir | sort -h >> $file && echo " ">> $file

# Huge Files:
echo "100 MB+ Files: ">> $file
find $dir -size +100M -exec ls -sh {} \; | sort -r >> $file && echo " ">> $file

# Image file extension counts:
echo "No of Image Files: ">> $file
echo -n "JPG : " >> $file
find $dir -iname "*.jpg" | wc -l >> $file

echo -n "PNG : " >> $file
find $dir -iname "*.png" | wc -l >> $file

echo -n "GIF : " >> $file
find $dir -iname "*.gif" | wc -l >> $file

echo -n "JPEG: ">> $file
find $dir -iname "*.jpeg" | wc -l >> $file

echo -n "WEBP: ">> $file
find $dir -iname "*.webp" | wc -l >> $file && echo " ">> $file

# Video file extension counts:
echo "No of Video Files: ">> $file
echo -n "WEBM: ">> $file
find $dir -iname "*.webm" | wc -l >> $file

echo -n "MPG : ">> $file
find $dir -iname "*.mpg" | wc -l >> $file

echo -n "3GP : ">> $file
find $dir -iname "*.3gp" | wc -l >> $file

echo -n "DAT : ">> $file
find $dir -iname "*.dat" | wc -l >> $file

echo -n "MP4 : ">> $file
find $dir -iname "*.mp4" | wc -l >> $file

echo -n "AVI : ">> $file
find $dir -iname "*.avi" | wc -l >> $file

echo -n "FLV : ">> $file
find $dir -iname "*.flv" | wc -l >> $file && echo " ">> $file

echo "###########################">> $file


blog comments powered by Disqus