Linux Bash Scripting Tutorial


IO Redirections

There are always 3 different files are open, stdin(keyboard), stdout(the screen) and stderr(error messages output to the screen) Readirection means caputuring output from a file, command, program, script or from code block. and sending it as output to another program,script or command. each open file gets a file descriptor

  • stdin 0
  • stdout 1
  • stderr 2
file descriptor can range from 3 to 9

IO-Redirections mainly used for non-interactive operations.Standard Operations involve Reading input from keyboard,displaying output on a terminal and error messages also on terminal, Instead of that IO-Redirections reads data from File or Pipe or code block or another program, and outputs to File or Pipe or to a code block or another program

IO-Redirection Operators
OperatorMeaningExampleDescription
>Output Redirection ls > a.out ls command output redirected to a.out
>>Output Redirection(append)ls >> a.outls command output appended to a.out file
<Input Redirectioncat < file cat command reads input from file



$ cat < file


if file exists in the current directory,it reads line by line and echos on the terminal
if file doesnot exists in the current directory,error bash: file1: No such file or directory

$ ls > file


output of ls command writes into file.if file aleady exists in the current directory,then it truncates the file and writes the command output
if file doesnot exists in the current directory,creates a file specified in the IO redirection

$ ls >> file


output of ls command appends into a file.if file aleady exists in the current directory,then it appends command output to the existing file
if file doesnot exists in the current directory,creates a file specified in the IO redirection

within Shell script how to set a variable with file content
var1=`cat file.txt`

var2=`<file1`

Device FileFile DescriptorAbbrevationDescription
/dev/stdin0stdin/dev/stdin: symbolic link to /proc/self/fd/0
/dev/stdout1stdout/dev/stdout: symbolic link to /proc/self/fd/1
/dev/stderr2stderr/dev/stderr: symbolic link to /proc/self/fd/2
/dev/null

for stdin default is keyboard
for stdout & stderr default is screen


Note: Redirection occurs before variable expansion,so variable containing redirection symbols must be expanded first using eval, otherwise, the redirection symbol is uninterpretted.



Pipelines

        Output of one command redirected to standard input of another command.This technique is called as Pipe, denoted as | symbol. A command line includes tow or more commands connected with pipes is called as Pipeline



Pipeline syntax:


$ ~> command1|command2|command3|command4

         Output of command1 becomes standard input of command2
output of command2 becomes input of command3
similarly output of command3 becomes input of command4.

choosing right command & combining them using a pipe technique is a programmers jab

$ ~> cat country.txt

Country		Capital 	ISD_CODE

USA		Washington	1
China		Beijing		86
Japan		Tokyo		81
India		Delhi		91

$ ~> cat country.txt | wc -l
6
$ ~> cat country.txt | tail -n +3
USA		Washington	1
China		Beijing		86
Japan		Tokyo		81
India		Delhi		91

$ ~> cat country.txt | tail -n +2 | wc -l
4

Convert lower case letters to upper case letters using tr command.



$ ~> echo "hello world!" | tr a-z A-Z
HELLO WORLD!

->Convert file content into upper case using tr command

$ ~> cat < sample.txt | tr [:lower:] [:upper:]

Find IP address of the system

	$ ~>	ifconfig wlp1s0 | grep 'inet'| head -n 1 | awk '{print $2}'
output:
	192.168.43.155

Redirection using file descriptors

commamdDescription
cmd >&nSend output of cmd to file descriptor n
cmd m>&nSame as above, the output usually goes to file descriptor m is sent to file descriptor n instead
cmd >&-Close standard output
cmd <&nTake input for cmd from file descriptor n
cmd m<&nSame as previous one,normally input comes from file descriptor m comes from file descriptor n
cmd <&-Close standard input

exec shell built-in command



    exec shell built-in command, executes commands in current process rather than launching new process.

In this program exec redirects standard input(usually keyboard keys) to file,the line it starts exec command to end of the file all read operations performed using this input-file


#!/usr/bin/bash
#execd.sh


exec <input-file

read name address pin

echo ${name} ${address} ${pin}

Advanced Redirections

commamdDescription
cmd << textHere Document
cmd <<< textHere string
cmd <> fileOpen file for reading and writing
cmd >| filesends output of cmd to file even if the shell's noclobber is enabled/set

Multiple Redirections

cmd 2> filesend staderr to file;stdout remain same on the screen
cmd > file 2>&1Send both stdout and stderr to same file
cmd >& fileSend both stdout and stderr to same file
cmd &> fileSend both stdout and stderr to same file(this is preferred form)
cmd >file1 2>file2Send standard output to file1 and standard error to file2

noclobber     prevent overwritting of files by redirection
bash -C

ADS