Bash: Difference between revisions
Content added Content deleted
(38 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
= Basics = |
= Basics = |
||
Source: [https://ss64.com/bash/syntax.html ss64.com] |
Source: [https://ss64.com/bash/syntax.html ss64.com],[https://learnxinyminutes.com/docs/bash/ 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 |
|||
*Variable |
|||
* sh (or the Shell Command Language) is a programming language described by the POSIX standard. |
|||
$1, $2, $3 are the positional parameters. |
|||
* It has many implementations (ksh88, dash, ...). bash can also be considered an implementation of sh (see below). |
|||
"$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}. |
|||
* 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. |
|||
"$*" 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. |
|||
;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 |
|||
<syntaxhighlight lang='bash'> |
|||
$0 # Name of the Bash script. |
|||
$1 - $9 # First 9 arguments to the Bash script. |
|||
$@ # All the arguments supplied to the Bash script, {$1, $2, $3 ...}. |
|||
$* # IFS expansion of all positional parameters, $1 $2 $3 .... |
|||
$# # How many arguments were passed to the Bash script. |
|||
$- # Current options set for the shell. |
|||
$$ # The process ID of the current script. |
|||
$_ # Most recent parameter (or the abs path of the command to start the current shell immediately after startup). |
|||
$IFS # Input field separator. |
|||
$? # The exit status of the most recently run process. |
|||
$! # PID of the most recent background command. |
|||
$USER # The username of the user running the script. |
|||
$HOSTNAME # The hostname of the machine the script is running on. |
|||
$SECONDS # The number of seconds since the script was started. |
|||
$RANDOM # Returns a different random number each time is it referred to. |
|||
$LINENO # Returns the current line number in the Bash script. |
|||
</syntaxhighlight> |
|||
*Using Variables in Bash: |
*Using Variables in Bash: |
||
<syntaxhighlight lang='bash'> |
|||
dir="/data/.folder/" |
|||
dir="/data/.folder/" |
|||
path="/home/system/Desktop/pending_files.txt" |
|||
path="/home/system/Desktop/pending_files.txt" |
|||
tree $dir | wc -l >> $path |
|||
tree $dir | wc -l >> $path |
|||
</syntaxhighlight> |
|||
*Correct way to declare variable |
*Correct way to declare variable |
||
<syntaxhighlight lang='bash'> |
|||
Variable="Some string" |
|||
Variable="Some string" |
|||
</syntaxhighlight> |
|||
*Wrong way to declare Varibales: |
*Wrong way to declare Varibales: |
||
<syntaxhighlight lang='bash'> |
|||
Variable = "Some string" ==> Bash will decide that Variable is a command it must execute |
|||
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 |
|||
</syntaxhighlight> |
|||
*Print first 10 characters: |
|||
<syntaxhighlight lang='bash'> |
|||
echo ${text:0:10} |
|||
</syntaxhighlight> |
|||
=== Builtin variables === |
|||
<syntaxhighlight lang='bash'> |
|||
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..." |
|||
</syntaxhighlight> |
|||
== Array == |
|||
<syntaxhighlight lang="bash"> |
|||
array0=(one two three four five six) # Declare an array with 6 elements |
|||
echo $array0 # Print first element => "one" |
|||
echo ${array0[0]} # Print first element => "one" |
|||
echo ${array0[@]} # Print all elements => "one two three four five six" |
|||
echo ${#array0[@]} # Print number of elements => "6" |
|||
echo ${#array0[2]} # Print number of characters in third element => "5" |
|||
echo ${array0[@]:3:2} # Print 2 elements starting from fourth => "four five" |
|||
</syntaxhighlight> |
|||
* Print all elements. Each of them on new line. |
|||
<syntaxhighlight lang="bash"> |
|||
for i in "${array0[@]}"; do |
|||
echo "$i" |
|||
done |
|||
</syntaxhighlight> |
|||
== Brace Expansion == |
|||
<syntaxhighlight lang='bash'> |
|||
echo {1..10} |
|||
echo {a..z} |
|||
</syntaxhighlight> |
|||
* Display Even Numbers |
|||
<syntaxhighlight lang='bash'> |
|||
for number in {2..99..2} # 2 is the low end, 99 is the high end, 2 is the step after each iteration. |
|||
do |
|||
echo $number |
|||
done |
|||
</syntaxhighlight> |
|||
== Builtin variables == |
|||
Both commands are quivalent |
|||
<syntaxhighlight lang='bash'> |
|||
echo "I'm in $(pwd)" # execs `pwd` and interpolates output |
|||
echo "I'm in $PWD" # interpolates the built in `$PWD` variable |
|||
</syntaxhighlight> |
|||
== Reading Input == |
|||
<syntaxhighlight lang='bash'> |
|||
echo "What's your name?" |
|||
read Name # Note we didn't declare a variable |
|||
echo Hello, $Name! |
|||
</syntaxhighlight> |
|||
== Expressions == |
|||
;Let |
|||
<syntaxhighlight lang='bash'> |
|||
let a=5+4 |
|||
echo $a # 9 |
|||
let "a = 5 + 4" |
|||
echo $a # 9 |
|||
let a++ |
|||
echo $a # 10 |
|||
let "a = 4 * 5" |
|||
echo $a # 20 |
|||
let "a = $1 + 30" |
|||
echo $a # 30 + first command line argument for script |
|||
</syntaxhighlight> |
|||
;Expr |
|||
<syntaxhighlight lang='bash'> |
|||
expr 5 + 4 # 9 |
|||
expr "5 + 4" # 5 + 4 |
|||
expr 5+4 # 5+4 |
|||
expr 5 \* $1 # 60 ( 5*first command line argument for script[12]) |
|||
expr 11 % 2 # 1 |
|||
a=$( expr 10 - 3 ) |
|||
echo $a # 7 |
|||
</syntaxhighlight> |
|||
;Double Parentheses |
|||
<syntaxhighlight lang='bash'> |
|||
echo $(( 10 + 5 )) # 15 |
|||
a=$(( 4 + 5 )) |
|||
echo $a # 9 |
|||
a=$((3+5)) |
|||
echo $a # 8 |
|||
b=$(( a + 3 )) |
|||
echo $b # 11 |
|||
b=$(( $a + 4 )) |
|||
echo $b # 12 |
|||
(( b++ )) |
|||
echo $b # 13 |
|||
(( b += 3 )) |
|||
echo $b # 16 |
|||
a=$(( 4 * 5 )) |
|||
echo $a # 20 |
|||
</syntaxhighlight> |
|||
;Length of a Variable |
|||
<syntaxhighlight lang='bash'> |
|||
a='Hello World' |
|||
echo ${#a} # 11 |
|||
b=4953 |
|||
echo ${#b} # 4 |
|||
</syntaxhighlight> |
|||
== Read Files == |
|||
<syntaxhighlight lang='bash'> |
|||
Contents=$(cat file.txt) |
|||
echo "START OF FILE\n$Contents\nEND OF FILE" |
|||
</syntaxhighlight> |
|||
== Use subshells to work across directories == |
|||
<syntaxhighlight lang='bash'> |
|||
(echo "First, I'm here: $PWD") && (cd someDir; echo "Then, I'm here: $PWD") |
|||
# First, I'm here: /home/test |
|||
# Then, I'm here: /home/test/scripts |
|||
pwd # still in first directory |
|||
</syntaxhighlight> |
|||
== Case Statements == |
|||
<syntaxhighlight lang="bash"> |
|||
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.";; |
|||
esac |
|||
</syntaxhighlight> |
|||
== Loops == |
|||
*General Syntax: |
|||
<syntaxhighlight lang="bash"> |
|||
for Variable in {1..3} |
|||
do |
|||
echo "$Variable" |
|||
done |
|||
</syntaxhighlight> |
|||
*Traditional for loop way: |
|||
<syntaxhighlight lang="bash"> |
|||
for ((a=1; a <= 3; a++)) |
|||
do |
|||
echo $a |
|||
done |
|||
</syntaxhighlight> |
|||
*Act on files: |
|||
<syntaxhighlight lang="bash"> |
|||
for i in file1 file2 |
|||
do |
|||
cat "$i" |
|||
done |
|||
</syntaxhighlight> |
|||
*Output from a command |
|||
<syntaxhighlight lang="bash"> |
|||
for i in $(ls) |
|||
do |
|||
cat "$i" |
|||
done |
|||
</syntaxhighlight> |
|||
*While loop: |
|||
<syntaxhighlight lang="bash"> |
|||
while [ true ] |
|||
do |
|||
echo "This is loop..." |
|||
break |
|||
done |
|||
</syntaxhighlight> |
|||
== Functions == |
|||
* Declaration: |
|||
<syntaxhighlight lang="bash"> |
|||
function foo () |
|||
{ |
|||
echo "Arguments work just like script arguments: $@" |
|||
echo "And: $1 $2..." |
|||
echo "This is a function" |
|||
return 0 |
|||
} |
|||
</syntaxhighlight> |
|||
foo arg1 arg2 |
|||
*Another way: |
|||
<syntaxhighlight lang="bash"> |
|||
bar () |
|||
{ |
|||
echo "Another way to declare functions!" |
|||
return 0 |
|||
} |
|||
</syntaxhighlight> |
|||
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= |
=Bash Scripts= |
||
Line 38: | Line 301: | ||
*Script to find file extensions |
*Script to find file extensions |
||
<syntaxhighlight lang='bash'> |
|||
find $dir -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u |
find $dir -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u |
||
</syntaxhighlight> |
|||
*Convert files with name "webp" file to Png: |
*Convert files with name "webp" file to Png: |
||
find . -name "*.webp" -exec dwebp {} -o {}.png \; |
find . -name "*.webp" -exec dwebp {} -o {}.png \; |
||
find . -name "*.webp" -delete |
find . -name "*.webp" -delete |
||
</syntaxhighlight> |
|||
* Create Multiple Directories with single command: |
* Create Multiple Directories with single command: |
||
<syntaxhighlight lang='bash'> |
|||
mkdir ep{1..16} |
mkdir ep{1..16} |
||
mkdir -p sa{0..10}/{1,2,3,4} |
mkdir -p sa{0..10}/{1,2,3,4} |
||
mkdir -p sa{0..10}/{1..4} |
mkdir -p sa{0..10}/{1..4} |
||
</syntaxhighlight> |
|||
*Download multiple files at once: |
*Download multiple files at once: |
||
<syntaxhighlight lang='bash'> |
|||
wget https://imagedump.com/i/1/234{1..100}.jpg |
wget https://imagedump.com/i/1/234{1..100}.jpg |
||
wget -i list.txt |
wget -i list.txt |
||
</syntaxhighlight> |
|||
*Saving text use echo to continue on same line: |
*Saving text use echo to continue on same line: |
||
<syntaxhighlight lang='bash'> |
|||
echo -n "Count: " >> files.txt |
echo -n "Count: " >> files.txt |
||
find . | wc -l >> files.txt |
find . | wc -l >> files.txt |
||
</syntaxhighlight> |
|||
* Word count for directories |
|||
<syntaxhighlight lang='bash'> |
|||
IFS=$'\n'; for i in `find . -type d`; do echo $i; ls $i|wc -l; done; unset IFS |
|||
</syntaxhighlight> |
|||
* Find Lengths of multiple video files: |
|||
<syntaxhighlight lang='bash'> |
|||
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 |
|||
</syntaxhighlight> |
|||
== Small Scripts == |
== Small Scripts == |
||
;Extract multiple tar files to sub-directories: |
|||
<syntaxhighlight lang='bash'> |
|||
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 |
|||
</syntaxhighlight> |
|||
;Tar Multiple files at once: |
|||
<syntaxhighlight lang='bash'> |
|||
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 |
|||
</syntaxhighlight> |
|||
;Checking Internet connectivity |
;Checking Internet connectivity |
||
<syntaxhighlight lang='bash'> |
|||
<pre> |
|||
#!/bin/bash |
#!/bin/bash |
||
Line 68: | Line 360: | ||
echo "Down" |
echo "Down" |
||
fi |
fi |
||
</syntaxhighlight> |
|||
</pre> |
|||
;Mplayer Play Live Gurbani from 8 to 10AM |
;Mplayer Play Live Gurbani from 8 to 10AM |
||
<syntaxhighlight lang='bash'> |
|||
<pre> |
|||
#!/bin/bash |
#!/bin/bash |
||
trickle -d 20 -u 15 mplayer mms://sgpc.net/live & |
trickle -d 20 -u 15 mplayer mms://sgpc.net/live & |
||
sleep 2h |
sleep 2h |
||
kill $! |
kill $! |
||
</syntaxhighlight> |
|||
</pre> |
|||
Cron Entry: |
Cron Entry: |
||
Line 87: | Line 379: | ||
http://newspaper.ajitjalandhar.com/newspages/20130226/20130226_1_1.jpg |
http://newspaper.ajitjalandhar.com/newspages/20130226/20130226_1_1.jpg |
||
<syntaxhighlight lang='bash'> |
|||
<pre style="width: 97%; overflow-x: scroll;"> |
|||
#!/bin/bash |
#!/bin/bash |
||
cd ~/Desktop |
cd ~/Desktop |
||
Line 98: | Line 390: | ||
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" |
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 |
fi |
||
</syntaxhighlight> |
|||
</pre> |
|||
Cron Entry to download daily: |
Cron Entry to download daily: |
||
Line 104: | Line 396: | ||
;Eye strain preventer: |
;Eye strain preventer: |
||
<pre> |
|||
<syntaxhighlight lang='bash'> |
|||
#!/bin/bash |
#!/bin/bash |
||
PATH=/usr/games:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
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 |
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 |
||
</syntaxhighlight> |
|||
</pre> |
|||
Add following line to startup programs: |
Add following line to startup programs: |
||
Line 116: | Line 409: | ||
;Extension Doctor |
;Extension Doctor |
||
{{notice|This script will overwrite any existing file.}} |
{{notice|This script will overwrite any existing file.}} |
||
<pre> |
|||
<syntaxhighlight lang='bash'> |
|||
for f in *.{jpg,JPG,png,PNG,jpeg,JPEG,webp}; do |
for f in *.{jpg,JPG,png,PNG,jpeg,JPEG,webp}; do |
||
type=$( file "$f" | grep -oP '\w+(?= image)' ) |
type=$( file "$f" | grep -oP '\w+(?= image)' ) |
||
Line 131: | Line 425: | ||
fi |
fi |
||
done |
done |
||
</syntaxhighlight> |
|||
</pre> |
|||
== Big Scripts == |
== Big Scripts == |
Latest revision as of 16:28, 10 December 2019
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
$0 # Name of the Bash script.
$1 - $9 # First 9 arguments to the Bash script.
$@ # All the arguments supplied to the Bash script, {$1, $2, $3 ...}.
$* # IFS expansion of all positional parameters, $1 $2 $3 ....
$# # How many arguments were passed to the Bash script.
$- # Current options set for the shell.
$$ # The process ID of the current script.
$_ # Most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$IFS # Input field separator.
$? # The exit status of the most recently run process.
$! # PID of the most recent background command.
$USER # The username of the user running the script.
$HOSTNAME # The hostname of the machine the script is running on.
$SECONDS # The number of seconds since the script was started.
$RANDOM # Returns a different random number each time is it referred to.
$LINENO # Returns the current line number in the Bash script.
- Using Variables in Bash:
dir="/data/.folder/"
path="/home/system/Desktop/pending_files.txt"
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..."
Array
array0=(one two three four five six) # Declare an array with 6 elements
echo $array0 # Print first element => "one"
echo ${array0[0]} # Print first element => "one"
echo ${array0[@]} # Print all elements => "one two three four five six"
echo ${#array0[@]} # Print number of elements => "6"
echo ${#array0[2]} # Print number of characters in third element => "5"
echo ${array0[@]:3:2} # Print 2 elements starting from fourth => "four five"
- Print all elements. Each of them on new line.
for i in "${array0[@]}"; do
echo "$i"
done
Brace Expansion
echo {1..10}
echo {a..z}
- Display Even Numbers
for number in {2..99..2} # 2 is the low end, 99 is the high end, 2 is the step after each iteration.
do
echo $number
done
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!
Expressions
- Let
let a=5+4
echo $a # 9
let "a = 5 + 4"
echo $a # 9
let a++
echo $a # 10
let "a = 4 * 5"
echo $a # 20
let "a = $1 + 30"
echo $a # 30 + first command line argument for script
- Expr
expr 5 + 4 # 9
expr "5 + 4" # 5 + 4
expr 5+4 # 5+4
expr 5 \* $1 # 60 ( 5*first command line argument for script[12])
expr 11 % 2 # 1
a=$( expr 10 - 3 )
echo $a # 7
- Double Parentheses
echo $(( 10 + 5 )) # 15
a=$(( 4 + 5 ))
echo $a # 9
a=$((3+5))
echo $a # 8
b=$(( a + 3 ))
echo $b # 11
b=$(( $a + 4 ))
echo $b # 12
(( b++ ))
echo $b # 13
(( b += 3 ))
echo $b # 16
a=$(( 4 * 5 ))
echo $a # 20
- Length of a Variable
a='Hello World'
echo ${#a} # 11
b=4953
echo ${#b} # 4
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")
# First, I'm here: /home/test
# Then, I'm here: /home/test/scripts
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.";;
esac
Loops
- General Syntax:
for Variable in {1..3}
do
echo "$Variable"
done
- Traditional for loop way:
for ((a=1; a <= 3; a++))
do
echo $a
done
- Act on files:
for i in file1 file2
do
cat "$i"
done
- Output from a command
for i in $(ls)
do
cat "$i"
done
- While loop:
while [ true ]
do
echo "This is loop..."
break
done
Functions
- 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
}
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
- 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
</syntaxhighlight>
- 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
- 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
#!/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
- Reference Guide: gnu.org
- Check Scripts for errors: shellcheck.net
{{#widget:DISQUS
|id=networkm
|uniqid=Bash
|url=https://aman.awiki.org/wiki/Bash
}}