Bash

= Basics = Source: ss64.com,learnxinyminutes.com


 * POSIX
 * 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
 * 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
 * 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.

Variables

 * Declaring


 * Using Variables in Bash:


 * Correct way to declare variable


 * Wrong way to declare Varibales:


 * Print first 10 characters:

Array

 * Print all elements. Each of them on new line.

Brace Expansion

 * Display Even Numbers

Builtin variables
Both commands are quivalent

Expressions

 * Let


 * Expr


 * Double Parentheses


 * Length of a Variable

Loops

 * General Syntax:


 * Traditional for loop way:


 * Act on files:


 * Output from a command


 * While loop:

Functions

 * Declaration:

foo arg1 arg2


 * Another way:

bar

Misc
Trap executes a command whenever script receives a signal (here any of the three listed signals): trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM

Sudo is used to run commands as the superuser: NAME1=$(whoami) NAME2=$(sudo whoami) echo "Was $NAME1, then became more powerful $NAME2"

=Bash Scripts=

One Liners
find $dir -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
 * Script to find file extensions

find. -name "*.webp" -exec dwebp {} -o {}.png \; find. -name "*.webp" -delete
 * Convert files with name "webp" file to Png:

mkdir ep{1..16} mkdir -p sa{0..10}/{1,2,3,4} mkdir -p sa{0..10}/{1..4}
 * Create Multiple Directories with single command:

wget https://imagedump.com/i/1/234{1..100}.jpg wget -i list.txt
 * Download multiple files at once:

echo -n "Count: " >> files.txt find. | wc -l >> files.txt
 * Saving text use echo to continue on same line:

IFS=$'\n'; for i in `find. -type d`; do echo $i; ls $i|wc -l; done; unset IFS
 * Word count for directories

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
 * Find Lengths of multiple video files:

Small Scripts

 * Extract multiple tar files to sub-directories:


 * Tar Multiple files at once:


 * Checking Internet connectivity


 * Mplayer Play Live Gurbani from 8 to 10AM

Cron Entry: 0 8 * * * ./mplayer.sh


 * Download Newspaper front page

URL: http://newspaper.ajitjalandhar.com/newspages/20130226/20130226_1_1.jpg

Cron Entry to download daily: 0 7-11 * * * ./ajit.sh


 * Eye strain preventer:

Add following line to startup programs: /home/aman/Scripts/eyesaver.sh >/dev/null 2>&1


 * Extension Doctor

Big Scripts

 * Directory Stats


 * References
 * Reference Guide: gnu.org
 * Check Scripts for errors: shellcheck.net