This repository contains notes and examples related to programming with Bash and Shell script in Unix like systems.
ShellMIT
Bash/ Shell script notes
This repository contains notes and examples related to programming with Bash and Shell script in Unix like systems. A
list of useful commands to perform tasks from the console, something that can often get us out of trouble in different
situations.
Contents
Linux commands.
Bash programming.
Special tricks.
1. Linux commands
Console
Command
Description
Example
history
Clean command history
history -c
.
Command execution
RESULT_FROM=$(/usr/bin/pwd);
.
Command execution (deprecated)
RESULT_FROM=`/usr/bin/pwd`;
User account (TODO)
Command
Description
Example
who
Who is connected
who
whoami
What is my account
whoami
Output redirection (TODO)
Managing files and directories
Command
Description
Example
pwd
Current directory
pwd
du
List space used in human readable format
du -h
du, sort, head
List files
List files and summarized and with blocks in Megas, sort numerically and in reverse to show the largest, select the first 20. du -sm * | sort -nr | head -20
find
Search files
Look for the string 'unit1' in the /etc directory and subdirectories in files of type f find /etc -depth -type f -exec grep -il unit1 {} \;
Replace a string in multiple files. find *.php -type f | xargs sed -i s/SEARCHED_NAME/REPLACED_NAME find . -type f | xargs sed -i s/SEARCHED_NAME/REPLACED_NAME/g
sed
Replace a string in a file.
Another example, search for the string {PROXY_USER} (full string including braces) in the settings.xml file, the result is printed to another file called new_settings.xml sed -s 's/${PROXY_USER}/MYVALUE/g' file_to_search.txt > new_replaced_file.xml
sed, find, xargs
Replace a string in multiple files
Replace a string in multiple files. * find *.php -type f | xargs sed -i s/SEARCHED_NAME/REPLACED_NAME * find . -type f | xargs sed -i s/SEARCHED_NAME/REPLACED_NAME/g
Check file integrity using md5sum and shasum
Command
Description
Example
shasum
Create hash SHA
shasum -b file.jpg > shasum.txt
shasum
Verify SHA
shasum -c shasum.txt.
md5sum
Create hash MD5
md5sum -b file.jpg > md5sum.txt
md5sum
Verify MD5
md5sum -c md5sum.txt
echo, md5sum
Convert string to MD5
* echo "HELLO" | md5sum * echo HELLO | md5sum
Check file integrity using md5sum and shasum
Command
Description
Example
iconv
Convert a file from latin1 to utf-8
iconv -f latin1 -t utf8 output_file > input_file
iconv
Convert a file from utf-8 to latin1
iconv -f utf8 -t latin1 input_file > output_file
Disk drive management
Command
Description
Example
lsblk
See block units
lsblk
lsusb
View existing usb drives with their names and file systems
lsusb
mount
Mount
mount -t iso9662 /dev/sdb1 /mnt/sdb1
mount
Mount .iso file as directory
* mount file.iso /mnt/my_directory/ -t iso9660 -o ro,loop=/dev/loop0 * mount -o loop dvd1.iso /mnt/isodisk
Having images in .png format and numbered with two digits at the end, a video file in .avi format is produced. Example: block01.png, block02.png, etc. ffmpeg -r 1 -i bloque_%02d.png video.avi
ffmpeg
Create a video from the terminal
Videos for tutorials can be recorded from the terminal. * ffmpeg -f x11grab -s 1024×768 -r 25 -i :0.0 -qscale 0 video.avi * ffmpeg -f x11grab -r 25 -i :0.0 -qscale 0 video.avi
split
Split video
Split large files into multiple pieces split -b4300m MY_VIDEO_FILE.avi MY_NEW_VIDEO_FILE_SPLITTED
cat
Merge video
Merge parts of separate files into a single file. cat MY_NEW_VIDEO_FILE_SPLITTED.* > MY_MERGED_VIDEO_FILE.avi
mencoder
Convert video
Convert a mpg video to avi (mencoder is part of Mplayer) mencoder INPUT_FILE.mpg -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=640:480 -sws 2 -oac copy -o OUTPUT_FILE.avi
.
.
. .
3. Bash programming
Script header
Sentence
Example
Bash
#!/bin/bash
Sh
#!/bin/sh
Variable definitions, print and read data
Command
Description
Example
Variable definition
MY_VARIABLE=1;
read
Read input from keyboard
read MY_VARIABLE;
echo
Print data
echo "Some text here -> "$MY_VARIABLE;
#!/bin/bash
echo "---- SCRIPT INFORMATION ---";
MY_VARIABLE=1;
echo "Enter value ->; read $MY_VARIABLE;
echo "Some text here -> "$MY_VARIABLE;
Script Parameters
Operator
Description
Example
$0
Script name
$$
PID number
$@
Array of arguments
$#
Number of arguments received
$*
Arguments received
$1
First parameter received from from command line
#!/bin/bash
echo "---- SCRIPT INFORMATION ---";
user_name=`whoami`
echo "Username:"$user_name;
echo "Script name: "$0;
echo "PID number:" $$;
echo "Array of parameters: "$@;
echo "Number of parameters: "$#;
echo "Arguments received:"$*;
Comparisons
Integer comparison
Operator
Description
Example
-gt
> Great than
-lt
< Less than
-ge
>= Great or equal
-lt
<= Less or equal
-eq
== Equal
-ne
!= Not equal
String comparison (TODO)
Operator
Description
Example
-eq
== Equal
-ne
!= Not equal
-gt
> Great than
-lt
< Less than
-ge
>= Great or equal
-lt
<= Less or equal
Boolean operators
Operator
Description
Example
&&
> Great than
| |
< Less than
Files and directories operators
Operator
Example
Check if is file
[ -f $filename ]
Check if is directory
[ -d $directory_name ]
Check if is exists
[ -e $filename ]
Is with write permission
[ -w $filename ]
Is with execution permission
[ -x $filename ]
Flow control sentences
If sentence
...
my_number_option=2
if [ $my_number_option = 1 ]
then
echo "True number!!";
else
echo "Else number!!";
fi
...
file='example_if.sh'
if [ -f $file ]
then
echo "is a file"
fi
if [ -d $file ]
then
echo "is a directory"
fi
While loop
LAST_NUMBER=10
COUNTER_INDEX=1
while [ $COUNTER_INDEX -le $LAST_NUMBER ]
do
echo $COUNTER_INDEX
COUNTER_INDEX=`expr $COUNTER_INDEX + 1`
done
For loop
Iterate over filenames in directory
for a_file in $( ls ); do
echo $a_file
done
Case statements
case $my_number_option in
1)
echo "1->"
;;
2)
echo "2->"
;;
*)
echo "Any option->"
;;
esac