Bash: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 129: Line 129:
== Big Scripts ==
== Big Scripts ==
;Directory Stats
;Directory Stats

<pre>
<syntaxhighlight lang="bash">
#!/bin/bash
#!/bin/bash
#Script to export file/directory stats to a file.
#Script to export file/directory stats to a file.
Line 201: Line 202:
echo "###########################">> $file
echo "###########################">> $file
exit
exit
</syntaxhighlight>
</pre>





Revision as of 10:54, 22 November 2017


Basics

Source: ss64.com

        This section is under construction.


  • Echo continue on same line:
echo -n "Count: " >> files.txt
find . | wc -l >> files.txt
  • Variable
$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:
dir="/data/.folder/"
path="/home/system/Desktop/pending_files.txt"
tree $dir | wc -l >> $path

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 https://imagedump.com/i/1/234{1..100}.jpg
wget -i list.txt

Medium Scripts

Checking Internet connectivity
#!/bin/bash

if ping -c 1 -W 2 google.com > /dev/null; then
 echo "Up"
else
 echo "Down"
fi
Mplayer Play Live Gurbani from 8 to 10AM
 #!/bin/bash
 trickle -d 20 -u 15 mplayer mms://sgpc.net/live &
 sleep 2h
 kill $!

Cron Entry:

0 8 * * * ./mplayer.sh
Download Newspaper front page

URL:

http://newspaper.ajitjalandhar.com/newspages/20130226/20130226_1_1.jpg
  
 #!/bin/bash
 cd ~/Desktop
 D=$(date +"%Y%m%d")
 FILE="$D"_1_1.jpg
 if [ -f $FILE ];
 then
    exit
 else
    wget "http://newspaper.ajitjalandhar.com/newspages/"$D"/"$D"_1_1.jpg" && notify-send -i /data/Softwares/Wallpapers/icons/ajit.png -t 50 "Ajit Jalandhar" "is saved at your desktop"
 fi

Cron Entry to download daily:

0 7-11 * * * ./ajit.sh
Eye strain preventer
#!/bin/bash
PATH=/usr/games:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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/eyesaver.sh >/dev/null 2>&1
Extension Doctor
        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 ;; 
    esac
    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"
    fi
done

Big Scripts

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

# Path Variables:
dir="/data/"
file="/home/aman/Desktop/pending_files.txt"

# 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
exit



References





{{#widget:DISQUS |id=networkm |uniqid=Bash |url=https://aman.awiki.org/wiki/Bash }}