Saturday 18 March 2017

Linux Tutorial : Basic Command Line Skills on Linux Part III (3)



11 Globbing

Glob characters are often referred to as "wild cards". These are symbols that have special meaning to the shell.
Unlike commands that the shell will run, or options and arguments that the shell will pass to commands, glob characters are interpreted by the shell itself before it attempts to run any command. This means that glob characters can be used with any command.
Globs are powerful because they allow you to specify patterns that match filenames in a directory, so instead of manipulating a single file at a time, you can easily execute commands that will affect many files. For instance, by using glob characters it is possible to manipulate all files with a certain extension or with a particular filename length.
Keep in mind that these globs can be used with any command because it is the shell, not the command that expands with globs into matching filenames. The examples provided in this chapter will use the echo command for demonstration.
Asterisk (*)
The asterisk character is used to represent zero or more of any character in a filename. For example, suppose you want to display all of the files in the /etc directory that begin with the letter t:
sysadmin@localhost:~$ echo /etc/t*
/etc/terminfo /etc/timezone
sysadmin@localhost:~$
The pattern t* means "match any file that begins with the character t and has zero or more of any character after the t".
You can use the asterisk character at any place within the filename pattern. For example, the following will match any filename in the /etc directory that ends with .d:
sysadmin@localhost:~$ echo /etc/*.d
/etc/apparmor.d /etc/bash_completion.d /etc/cron.d /etc/depmod.d /etc/fstab.d /etc/init.d /etc/insserv.conf.d /etc/ld.so.conf.d /etc/logrotate.d /etc/modprobe.d /etc/pam.d /etc/profile.d /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d /etc/rcS.d /etc/rsyslog.d /etc/sudoers.d /etc/sysctl.d /etc/update-motd.d
In the next example, all of the files in the /etc directory that begin with the letter r and end with .conf will be displayed:
sysadmin@localhost:~$ echo /etc/r*.conf
/etc/resolv.conf /etc/rsyslog.conf

Question Mark (?)

The question mark represents any one character. Each question mark character matches exactly one character, no more and no less.
Suppose you want to display all of the files in the /etc directory that begin with the letter tand have exactly 7 characters after the t character:
sysadmin@localhost:~$ echo /etc/t???????
/etc/terminfo /etc/timezone
sysadmin@localhost:~$
Glob characters can be used together to find even more complex patterns. The echo /etc/*???????????????????? command will print only files in the /etc directory with twenty or more characters in the filename:
sysadmin@localhost:~$ echo /etc/*????????????????????
/etc/bindresvport.blacklist /etc/ca-certificates.conf
sysadmin@localhost:~$
The asterisk and question mark could also be used together to look for files with three-letter extensions by running the echo /etc/*.??? command:
sysadmin@localhost:~$ echo /etc/*.???
/etc/blkid.tab /etc/issue.net
sysadmin@localhost:~$
Brackets []
Brackets are used to match a single character by representing a range of characters that are possible match characters. For example, echo /etc/[gu]* will print any file that begins with either a g or u character and contains zero or more additional characters:
sysadmin@localhost:~$ echo /etc/[gu]*
/etc/gai.conf /etc/groff /etc/group /etc/group- /etc/gshadow /etc/gshadow- /etc/ucf.conf /etc/udev /etc/ufw /etc/update-motd.d /etc/updatedb.conf
sysadmin@localhost:~$
Brackets can also be used to a represent a range of characters. For example, the echo /etc/[a-d]* command will print all files that begin with any letter between and including aand d:
sysadmin@localhost:~$ echo /etc/[a-d]*
/etc/adduser.conf /etc/adjtime /etc/alternatives /etc/apparmor.d
/etc/apt /etc/bash.bashrc /etc/bash_completion.d /etc/bind /etc/bindresvport.blacklist /etc/blkid.conf /etc/blkid.tab /etc/ca-certificates /etc/ca-certificates.conf /etc/calendar /etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly /etc/cron.weekly /etc/crontab /etc/dbus-1 /etc/debconf.conf /etc/debian_version /etc/default
/etc/deluser.conf /etc/depmod.d /etc/dpkg
sysadmin@localhost:~$
The echo /etc/*[0-9]* command would display any file that contains at least one number:
sysadmin@localhost:~$ echo /etc/*[0-9]*
/etc/dbus-1 /etc/iproute2 /etc/mke2fs.conf /etc/python2.7 /etc/rc0.d
/etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d
sysadmin@localhost:~$
The range is based on the ASCII text table. This table defines a list of characters, arranging them in a specific standard order. If you provide an invalid order, no match will be made:
sysadmin@localhost:~$ echo /etc/*[9-0]*
/etc/*[9-0]*
sysadmin@localhost:~$
Exclamation Point (!)
The exclamation point is used in conjunction with the square brackets to negate a range. For example, the command echo [!DP]* will display any file that does not begin with a D or P.

12 Quoting

There are three types of quotes that have special significance to the Bash shell: double quotes ", single quotes ', and back quotes `. Each set of quotes indicates to the shell that it should treat the text within the quotes differently than it would normally be treated.

Double Quotes

Double quotes will stop the shell from interpreting some metacharacters, including glob characters. Within double quotes an asterisk is just an asterisk, a question mark is just a question mark, and so on. This means that when you use the second echo command below, the BASH shell doesn't convert the glob pattern into filenames that match the pattern:
sysadmin@localhost:~$ echo /etc/[DP]*
/etc/DIR_COLORS /etc/DIR_COLORS.256color /etc/DIR_COLORS.lightbgcolor /etc/PackageKit
sysadmin@localhost:~$ echo "/etc/[DP]*"
/etc/[DP]*
sysadmin@localhost:~$
This is useful when you want to display something on the screen that is normally a special character to the shell:
sysadmin@localhost:~$ echo "The glob characters are *, ? and [ ]"
The glob characters are *, ? and [ ]
sysadmin@localhost:~$
Double quotes still allow for command substitution (discussed later in this chapter), variable substitution and permit some other shell metacharacters that haven't been discussed yet. For example, in the following demonstration, you will notice that the value of the PATH variable is displayed:
sysadmin@localhost:~$ echo "The path is $PATH"
The path is /usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
sysadmin@localhost:~$

Single Quotes

Single quotes prevent the shell from doing any interpreting of special characters. This includes globs, variables, command substitution and other metacharacter that have not been discussed yet.
For example, if you want the $ character to simply mean a $, rather than it acting as an indicator to the shell to look for the value of a variable, you could execute the second command displayed below:
sysadmin@localhost:~$ echo The car costs $100
The car costs 00
sysadmin@localhost:~$ echo 'The car costs $100'
The car costs $100
sysadmin@localhost:~$

Backslash Character (\)

You can use an alternative technique to essentially single quote a single character. For example, suppose you want to print the following: “The services costs $100 and the path is $PATH". If you place this in double quotes, $1 and $PATH are considered variables. If you place this in single quotes,$1and $PATH are not variables. But what if you want to have $PATH treated as a variable and $1 not?
If you place a backslash \ character in front of another character, it treats the other character as a "single quoted" character. The third command below demonstrates using the \ character while the other two demonstrate how the variables would be treated within double and single quotes:
sysadmin@localhost:~$ echo "The service costs $100 and the path is $PATH"
The service costs 00 and the path is /usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
sysadmin@localhost:~$ echo 'The service costs $100 and the path is $PATH'
The service costs $100 and the path is $PATH
sysadmin@localhost:~$ echo The service costs \$100 and the path is $PATH
The service costs $100 and the path is /usr/bin/custom:/home/sysadmin/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
sysadmin@localhost:~$

Back Quotes

Back quotes are used to specify a command within a command, a process called command substitution. This allows for very powerful and sophisticated use of commands.
While it may sound confusing, an example should make things more clear. To begin, note the output of the date command:
sysadmin@localhost:~$ date
Mon Nov 2 03:35:50 UTC 2015
Now note the output of the echo Today is date command line:
sysadmin@localhost:~$ echo Today is date
Today is date
sysadmin@localhost:~$
In the previous command the word date is treated as regular text and the shell simply passes date to the echo command. But, you probably want to execute the date command and have the output of that command sent to the echo command. To accomplish this, you would run the echo Today is `date`command line:
sysadmin@localhost:~$ echo Today is `date`
Today is Mon Nov 2 03:40:04 UTC 2015
sysadmin@localhost:~$

13 Control Statements

Control statements allow you to use multiple commands at once or run additional commands, depending on the success of a previous command. Typically these control statements are used within scripts, but they can also be used on the command line as well.

Semicolon

The semicolon can be used to run multiple commands, one after the other. Each command runs independently and consecutively; no matter the result of the first command, the second will run once the first has completed, then the third and so on.
For example, if you want to print the months of January, February and March of 2015, you can execute cal 1 2015; cal 2 2015; cal 3 2015 on the command line:
sysadmin@localhost:~$ cal 1 2015; cal 2 2015; cal 3 2015
January 2015
Su Mo Tu We Th Fr Sa
4 5 6 7 8 9 10
1 2 3
18 19 20 21 22 23 24
11 12 13 14 15 16 17
25 26 27 28 29 30 31
1 2 3 4 5 6 7
February 2015 Su Mo Tu We Th Fr Sa
22 23 24 25 26 27 28
8 9 10 11 12 13 14 15 16 17 18 19 20 21
1 2 3 4 5 6 7
March 2015 Su Mo Tu We Th Fr Sa 8 9 10 11 12 13 14
29 30 31
15 16 17 18 19 20 21
22 23 24 25 26 27 28

Double Ampersand (&&)

The double ampersand && acts as a logical "and" if the first command is successful, then the second command (to the right of the &&) will also run. If the first command fails, then the second command will not run.
To better understand how this works, consider first the concept of failure and success for commands. Commands succeed when they work properly and fail when something goes wrong. For example, consider the ls /etc/xmlcommand line. The command will succeed if the /etc/xml directory is accessible and fail if it isn't.
For example, the first command will succeed because the /etc/xml directory exists and is accessible while the second command will fail because there is no /junk directory:
sysadmin@localhost:~$ ls /etc/xml
catalog catalog.old xml-core.xml xml-core.xml.old
sysadmin@localhost:~$ ls /etc/junk
ls: cannot access /etc/junk: No such file or directory
sysadmin@localhost:~$
The way that you would use the success or failure of the ls command in conjunction with && would be to execute a command line like the following:
sysadmin@localhost:~$ ls /etc/xml && echo success
catalog catalog.old xml-core.xml xml-core.xml.old
success
sysadumin@localhost:~$ ls /etc/junk && echo success
ls: cannot access /etc/junk: No such file or directory
sysadmin@localhost:~$
In the first example above, the echo command executed because the lscommand succeeded. In the second example, the echo command wasn't executed because the ls command failed.

Double Pipe

The double pipe || is a logical "or". It works in a similar way to &&; depending on the result of the first command, the second command will either run or be skipped.
With the double pipe, if the first command runs successfully, the second command is skipped; if the first command fails, then the second command will be run. In other words, you are essentially telling the shell, "Either run this first command or the second one”.
In the following example, the echo command will only execute if the lscommand fails:
sysadmin@localhost:~$ ls /etc/xml || echo failed
catalog catalog.old xml-core.xml xml-core.xml.old
sysadmin@localhost:~$ ls /etc/junk || echo failed
ls: cannot access /etc/junk: No such file or directory
failed
sysadmin@localhost:~$

Finish .....

Sumber : www.netacad.com

Load disqus comments

0 comments