Meta characters in Linux
1.1 METACHARACTERS
• The shell has a big family of metacharacters.
• Metacharacters are special characters to which special significance has been given. • All metacharacters can be classified into different groups as shown below table 3.1.
Table 3.1: metacharacters classification
1.2 FILENAME SUBSTITUTION METACHARACTERS:
• Filename substitution characters are ?, *, […..] , [!.....]
• These characters are used to for matching filenames in a directory.
• Each character has a special meaning as shown below.
Examples:
ls a* Lists all files beginning with character ‘a’.
ls ?? Lists all files whose names are 2 characters long.
ls a?b? Lists all 4-character filenames whose first character is ‘a’ and third character is ‘b’.
ls [kdgp]* Lists all files whose first character is ‘k’, ‘d’, ‘g’ or ‘p’.
Sample Outputs:
1.3 I/O REDIRECTION METACHARACTERS:
• I/O Redirection characters are >, <, >>, <<, |.
• these special characters specify from where input is to be picked up and where the output is to be sent.
1.3.1 output redirection:
The ‘>’ , ‘>>’ symbols are used for output redirection.
Example:
ls -l > listings
Here the output of command ls -l is re-directed to file "listings" instead of your screen.
the following who command which redirects the complete output of the command in the users file.
$ who > users
Notice that no output appears at the terminal. This is because the output has been redirected from the default standard output device (the terminal) into the specified file. You can check the users file for the complete content.
$ cat users
ramu tty01 Sep 12 07:30
gopi tty15 Sep 12 13:32
krishna tty21 Sep 12 10:10
patas tty24 Sep 12 13:07
steves tty25 Sep 12 13:03
$
If a command has its output redirected to a file and the file already contains some data, that data will be lost. Consider the following example −
$ echo line 1 > users
$ cat users
line 1
$
You can use >> operator to append the output in an existing file as follows −
$ echo line 2 >> users
$ cat users
line 1
line 2
$
1.3.2 input redirection:
The '<', << symbols are used for input redirection. The commands that normally take their input from the standard input can have their input redirected from a file in this manner. For
example, to count the number of lines in the file users generated above, you can execute the command as follows −
$ wc -l users
2 users
$
Upon execution, you will receive the following output. You can count the number of lines in the file by redirecting the standard input of the wc command from the file users −
$ wc -l < users
2
$
Note that there is a difference in the output produced by the two forms of the wc command. In the first case, the name of the file users is listed with the line count; in the second case, it is not.
In the first case, wc knows that it is reading its input from the file users. In the second case, it only knows that it is reading its input from standard input so it does not display file name.
1.3.3 pipes:
Pipes are used to redirect a stream from one program to another. It takes output from one program, or process, and sends it to another. When a program’s standard output is sent to another through a pipe, the first program’s output will not be displayed on the terminal instead it goes to the second program as input. The Linux pipe is represented by a vertical bar.so by using pipes You can connect two commands together so that the output from one program becomes the input of the next program.
Syntax: command_1 | command_2 | command_3 | .... | command_N Example: count the number of users currently login.
$who | wc -l.
here the first command is who gives us the number of users currently working on the system, now this output is send to the second command wc -l as input.so the wc -l command count the lines (each user information is displayed in new line) and produces desired result.
Sample Outputs:
1.4 MORE ON I/O REDIRECTION:
• Linux defines three standard streams that are used by commands.
⮚ standard input (fd is 0 for standard input)
⮚ standard output (fd is 1 for standard output)
⮚ standard error (fd is 2 for standard error)
• each command takes its input from a stream known as standard input. • each command sends its output to a stream known as standard output. • if the command encounters an error, it is sent to standard error.
• when a command/program is initiated, the shell opens all the three standard I/O files automatically and attaches them to the commands as shown in below Figure 1.4.1
Figure 1.4.1: standard I/O files
• the three streams are denoted by the numbers 0,1,2.
• Linux allows users to change the standard input and output devices temporarily by using redirection and piping.
• the symbol > implies redirection of output and symbol < implies redirection of input.
• the > operator tells Linux don’t display this output on screen instead put it somewhere else.”
• the < operator tells Linux the input for this command is not coming from the keyboard this time look for it somewhere else.
• the somewhere else can be a file or a printer for output redirection and a file for input redirection.
$cat file1>file2
• the redirection operator declares file2 to be the standard output. the output of cat normally sent to screen is now sent to file2.
• if the file2 does not exist, it is created, if it is does exist, it is wiped clear and refilled with the new data.
• note that this change in standard output is temporary. when the command ends your terminal once again becomes the standard output.
$cat file1>>file2
• it is similar to >, except if the target file already exists, the new output is appended to its end.
now let’s look at input redirection.
$cat<newfile
• here the newfile became the standard input and cat read its contents and displayed them on the screen.
• the same thing could have been achieved by saying,
$cat newfile
• though both the commands produce the same output there is a subtle difference between the two.in the second example the newfile was not the standard input. instead, the standard input remained the terminal, and the internal programming of cat caused the newfile to be opened in addition to the standard input.
• with the next operator <<, you can select the input from the specified source. $cat <<deadline
• the above command will continue to read from the standard input only till the word deadline is not fed in on a fresh line.
• cat will terminate if it encounters a fresh line containing ‘deadline’ as shown below.
• one more redirection operator is m>filename
• the best use of m>filename is when we want to redirect all our error messages to some file.
$cmd 2>errors
• the file descriptor 2 signifies the standard error which is now set to a file called errors.
• another redirection operator is m>&n.
• time command is used to find out the time taken by a process to execute. • the output of time command always comes to the standard error device, which is screen.
• if we want to output the time to some file, the following command will not suffice $time ls>newfile
• to send the time of execution to newfile we need to say,
$time ls>newfile 2>&1
• this merges the standard output and standard error.as a result whatever goes to the standard error would now be redirected to newfile.
• tee command sends a copy of its input to one or more files as well as standard output.
1.5 PROCESS EXECUTION METACHARACTERS:
• Process execution characters are ; () & && ||.
• These characters help with different ways of execution of commands. • when we want to run the more than one command at shell prompt, we separate them with a semicolon. For example
$ls;who;date
The above command result in the execution ls first, then who and lastly date. • if we wish certain commands to be executed in a sub-shell, we enclose them in a parentheses.
$ (cd mydir;pwd)
/usr/aa1/mydir
$pwd
/usr/aa1
The parentheses cause a sub-shell to be invoked, in which a change of directory is executed. After executing a pwd in that sub-shell, the sub-shell ceases to exit. Back in the current shell, we are still in /usr/aa1.
• The metacharacter which delegates a given process execution to the background is &.
• If you want a long file to be sorted, and we know that it is going to take quite some time to sort this file, then we can mark it for background execution while we continue to work on something else.
$ sort abcd>file2&
Sample Output:
1.5.1 conditional execution using &&, ||
• these metacharacters can be used to execute a command based on the success or failure of the previous command.
• if we want second command to be executed only if the first succeeds, we say $command 1&&command 2
• if we want second command to be executed only if the first one fails, we say $command 1||command 2
Sample Output:
1.6 QUOTING METACHARACTERS
• Quoting metacharacters are \ “… ” ‘…. ’ ` cmd`
1.6. 1 Escaping
When the \ precedes a metacharacter, its special meaning is turned off. In the pattern \*, the \ tells the shell that the asterisk has to be treated and matched literally instead of being interpreted as a metacharacter. This feature is known as escaping.
• \ takes away a special significance attached to any metacharacter.
• Assume we want to print a message like “this is *” , we normally say $echo this is *
• But here the shell interprets the * at the end of the line as all the files in the current working directory. Hence instead of echoing * it echoes all the files in the current directory.
• escaping \ itself: Sometimes you may need to interpret the \ itself literally. You need another \ before it.
• the back quotes are used for command substitution. the backquotes replace the command they enclose with its output.
1.6.2 Quoting:
There’s another way to turn off the meaning of a metacharacter. When a command argument is enclosed in quotes, the meanings of all enclosed special characters are turned off.
The following example shows the protection of four special characters using single quotes:
$ echo ‘The characters |, and $ are also special’
The characters |, and $ are also special
We could have used escaping here, but then we would need to use a \ in front of each of these four metacharacters. We used single quotes because they protect all special characters (except the single quote). Double quotes are more permissive; they don’t protect (apart from the double quote itself) the $ and the ` (backquote).
Sample Outputs:
1.7 POSITIONAL PARAMETERS AND SPECIAL PARAMETERS
The shell reserves some variables for its use. $1 to $9 are nine shell variables called Positional parameters, which automatically collects the arguments passed at the command line. we will discuss about these parameters in detail at shell programming.
In addition to positional parameters some special parameters are designated by the shell for yielding information about the environment. the list of such parameters is:
• $*-stores complete set of positional parameters.
• $#-specifies the number of arguments.
• $0-holds the program name itself.
• $$-specifies process id of the current shell.
• $!- specifies process id of the background job.
• $?- specifies exit status of the last executed command
The parameter $? stores the exit status of the last command. It has the value 0 if the command succeeds and a nonzero value if it fails.
Sample Output:
1.8 SHELL VARIABLES:
The shell supports variables that are useful both in the command line and shell scripts. there are two types of variables available in shell:
• user defined and
• system variables
System variables are created and maintained by Linux itself. Normally this type of variables defined in CAPITAL LETTERS. User defined variables (UDV) are Created and maintained by users.
1.8.1 User defined variables:
To define UDV use the following syntax
Syntax: variablename=value
e.g. $ no=10 $ n=10 $ vech=Bus
Rules for Naming variable name (Both UDV and System Variable)
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character.
e.g. Valid shell variable are as follows HOME, SYSTEM_VERSION, vech, no.
(2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g.. In following variable declaration there will be no error
$ no=10
But here there will be problem for following:
$ no =10 $ no= 10 $ no = 10
(3) Variables are case-sensitive, just like filenames in Linux. For e.g.
$ no=10 $No=11
(4) You can define NULL variable as follows:
$ vech=
$ vech=""
How to print or access value of UDV (User defined variables)
to print or access UDV use following syntax
Syntax: $variablename
For eg. To print the value of variable ‘vech’ ,we say $ echo $vech
Sample Output:
Local and Global Shell variable (export command)
Normally all our variables are local. Local variable can be used in same shell, if you load another copy of shell (by typing the sh at the $ prompt) then new shell ignores all old shell's variables.
• For e.g. Consider following example $ vech=Bus
$ echo $vech
Bus
$ sh
$ echo $vech
NOTE:-Empty line printed.
Sample Output:
global variables (export command)
We can copy old shell's variable to new shell (i.e. first shells variable to seconds shell), such variable is known as Global Shell variable. To do this use export command (value is accessible in sub shells)
Syntax: export variable1, variable2,.....variableN
For e.g. $ vech=Bus
$ echo $vech ---- > Bus
$ export vech
$ sh
$ echo $vech ------ > Bus
$ exit
Sample Output:
1.8.2 system variables:
In Linux systems variables are a set of dynamic named values, stored within the system that are used by applications launched in shells or subshells. In simple words, a system variable is a variable with a name and an associated value.
The shell built-in command set displays the values of all your defined system variables. some examples and its meanings are shown in below table:
When you type any command on the command prompt, the shell has to locate the command before it can be executed. The PATH variable specifies the locations in which the shell should look for commands.
Sample Output:
The characters that the shell displays as your command prompt are stored in the variable PS1. The default secondary prompt is > (the greater than sign), but can be changed by re-defining the PS2 shell variable. When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to complete the command.
Sample Output:
1.9 CREATING NEW COMMANDS:
Linux operating system allows users to create commands and execute them over the command line.
Given a sequence of commands that is to be repeated more than few times.it would be convenient to make it into a “new” command with its own name, so you can use it like a regular command. Suppose you want to count users frequently with pipeline you say
$who | wc -l
Now let us create a new command(nu) which results the number of users. First step: create an ordinary file and save the required command in it. $echo ‘who|wc –l’>nu
Second step: make the file nu executable.
$chmod a+x nu
Third step: execute the file like any other command.
$nu
but it will not produce any result, because the command will not be found by shell. we know that The $PATH is a environmental variable which contains colon-delimited list of directories that tells the shell which directories to search for executable files.so the fourth step is add your new command(nu) parent directory to PATH variable.
Fourth step: add the new command(nu) to your PATH.
$mkdir bin
$mv nu bin
now add the path /home/gnanendra/bin to the search PATH by following commands. $PATH=“/home/gnanendra/bin:$PATH” ( or)
$PATH="$HOME/bin:$PATH“
Fifth step: Now, execute the command(nu) just like any other command, now it displays the count of users.
$nu
Example 1:
Create a new command to print number of files/sub directories in the directory. 1) echo ‘echo *|wc -w’>nf
2) chmod +x nf
3) move the new command to bin directory.
4) Add the path (required only once ).
5) Execute the command nf.
Sample Output:
Example 2:
Create a new command to change the file permissions of a file.
1) echo ‘chmod +x sample’>cx
2) chmod +x cx
3)move to bin directory
4)add the directory to PATH (if already done,not required)
5)execute cx
Sample Output:
If you execute the above command cx every time it changes the permission of a single file ‘sample’. But in real time the ideal case is you have to pass new filename every time when you execute cx.so, you must tell cx, what is the name of the file is?
$chmod +x filename
the solution is positional parameters ($1 to $9),which collects the arguments passed at command line.so, replace the old content of cx with
$chmod +x $1
and execute the cx command like
$cx sample $cx file1 $cx nf
1)echo ‘chmod +x $1’>cx
2)chmod +x cx
3)mv cx bin
4)cx sample
what if we want to handle more than one argument?
echo ‘chmod +x $1 $2 $3’>cx
echo ‘chmod +x $1 $2 $3 $4 $5 $6 $7 $8 $9’>cx
it only works upto $9. if you want more than 9 parameters the solution is chmod +x $*
Sample Output:
1.10 REVIEW QUESTIONS:
I. Short Answer Questions:
1) What is Escaping?
2) What is the use of metacharacter &?
3) Write about input redirection?
4) Write about output redirection?
5) List positional parameters?
6) Write about shell user defined variables?
7) Explain about environment variable PATH?
8) Give an example for creating new command.
9) Write about Filename substitution Metacharacters?
10) Explain about global variables?
II. Long Answer Questions:
1) Explain about I/O Redirection in detail?
2) Explain the process of creating new commands with examples? 3) Explain about Process execution met characters in detail?
4) Explain about Quoting in detail with examples?
5) Explain about system variables?
3.11 MULTIPLE CHOICE QUESTIONS:
1. What is the command to list all files starts with character ‘a’ extension with ‘c’ a)ls a*.c b)ls a??.c c)ls a[a-z].c d)all
2) Command to append the content file1 to file2 is
a)cat file1 b)cat file1 > file2 c)cat file1 >> file2 d) cat file3 >> file1 3) how to make a fileexecutable
a)chmod +r filename b)chmod +w file name c) chmod +x filename 4)whenever a command or run a file, Operating system will check a) bin directory b)etc directory c)current directory
5) whenever a command or run a file, Operating system will check in which order a)current directory, all directories mentioned in PATH
b) all directories mentioned in PATH, current directory
6) Command to overwrite the content file1 to file2 is
a)cat file1 b)cat file1 > file2 c)cat file1 >> file2 d) cat file3 >> file1
7)List all files starts with character and with extension ‘cob’
a)ls a*.cob b)a*.c c)[a-z]*.cob
8)list all files of two character names and with extension ‘cob’
a)a*.cob b)a{a-z].cob c)a??.cob
9)The super user is allowed to access any file in file system
a)yes b)no
10) The file name is 4 characters and starts with ‘ab’ and extension is ‘cob’ a)a*.cob b)ab.cob c)ab*.cob d)ab??.cob
11)cat bar 2>errorfile
a)sends 2 to error file b)sends output to 2>errorfile
c)sends error to errorfile
ANSWERS
1.[A] 2.[C] 3.[C] 4.[C] 5.[A] 6.[B] 7.[C] 8.[C] 9.[A] 10[D] 11[C]
0 Comments