I/O Redirection and
file descriptors
I/O Redirection and File Descriptors
• I/O redirectors are used to send output of
command to file or to read input from file.
• input redirection ; output redirection
• $ cal > mycal
• $ cat > myf
This is my file
^D (press CTRL + D to save file)
• $ sort
10
-20
11
2
^D
•
-20
2
10
11
File Descriptors
Standard File Descriptors Use Example
File number
as Standard
stdin 0 input Keyboard
stdout 1 as Standard Screen
output
as Standard
stderr 2 error Screen
• By default in Linux every program has three
files associated with it, (when we start our
program these three files are automatically
opened by your shell).
• The use of first two files (i.e. stdin and stdout),
are already seen by us.
• The last file stderr (numbered as 2) is used by
our program to print error on screen.
• $ rm bad_file_name111
• $ rm bad_file_name111 > er
• $ cat er
• $ rm bad_file_name111 2>er
• $ cat er
Functions
#define the function
hello_world ()
{
echo "hello world"
}
#call the function
hello_world
#define the function
hello_world () {
echo "Your first name is: $1"
echo "Your last name is: $2"
}
#call the function
hello_world Arun Kumar
add_num ()
{
sum=$(( $1+$2 ))
echo "Total: $sum"
}
read -p "Enter number 1: " num1
read -p "Enter number 2: " num2
add_num num1 num2
Local and Global Variables
$ vech=Bus
$ echo $vech
Bus
$ /bin/bash
$ echo $vech
$echo $SHELL
$ vech=Bus
$ echo $vech
Bus
$ export vech
$ /bin/bash
$ echo $vech
Bus
$ exit
$ echo $vech
Bus
File Handling
read -p "Enter file name : " filename
while read line
do
echo $line
done < $filename
echo Enter the filename
read file
c = `cat $file | wc -c`
w = `cat $file | wc -w`
l = `grep -c "." $file`
echo Number of characters in $file is $c
echo Number of words in $file is $w
echo Number of lines in $file is $l
echo -e "Enter the name of the file : \c"
read file_name
if [ -b $file_name ]
then
echo "$file_name is a block special file"
else
echo "$file_name is not a block special file"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -d $file_name ]
then
echo "$file_name is a directory"
else
echo "$file_name is not a directory"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -e $file_name ]
then
echo "$file_name exist"
else
echo "$file_name not exist"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -f $file_name ]
then
echo "$file_name is file"
else
echo "$file_name is not file"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -r $file_name ]
then
echo "$file_name is readable"
else
echo "$file_name is not readable"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -s $file_name ]
then
echo "$file_name has size > 0"
else
echo "$file_name has size = 0"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -w $file_name ]
then
echo "$file_name is writable"
else
echo "$file_name is not writable"
fi
echo -e "Enter the name of the file : \c"
read file_name
if [ -x $file_name ]
then
echo "$file_name is executable"
else
echo "$file_name is not executable"
fi
Bitwise Operators
Bitwise NOT Operator (~)
Bitwise AND Operator (&, &=)
Bitwise OR Operator (|, |=)
Bitwise XOR Operator (^, ^=)
Left Bitwise Shift (<<, <<=)
Right Bitwise Shift (>>, >>=)
• Bitwise NOT Operator (~)
• a = 15
• result = $((~a))
• Bitwise AND Operator (&, &=)
• read x y
• r1 = $ (( x & y ))
• echo r1
• Bitwise OR Operator (|, |=)
• read x y
• r1 = $ (( x | y ))
• echo r1
• Bitwise XOR Operator (^, ^=)
• read x y
• r1 = $ (( x ^ y ))
• echo r1
• Left Bitwise Shift (<<, <<=)
• read x
• r1 = $ (( x << 2 ))
• echo r1
• Right Bitwise Shift (>>, >>=)
• read x
• r1 = $ (( x >> 2 ))
• echo r1
Date Functions
$ date
$ date "+%D“
$ date "+%D %T“
$ date "+%Y-%m-%d“
$ date "+%Y/%m/%d“
• $ date --date="2 year ago“
• $ date --date="2 month ago“
• $ date --date="5 sec ago“
• $ date –date=”10 day ago”
• $ date --date="yesterday“
$date --date="next tue“
$date --date="2 day“
$date --date="tomorrow“
$date --date="1 year“
$date --set="Tue Nov 13 15:23:34 PDT 2018"
Popular Text Editors
in Linux
sudo yum install gedit
sudo yum install nano
sudo yum install emacs
sudo yum install sublime-text
sudo yum install vi
sudo yum install vim