Linux - chapter 4

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Bash Shell

A BASH shell variable is a feature that allows you or the shell to store data. This data can be used to provide critical system information or to change the behavior of how the BASH shell (or other commands) work. Variables are given names and stored temporarily in memory. When you close a terminal window or shell, all of the variables are lost. However, the system automatically recreates many of these variables when a new shell is opened.

home directory

A place unique to your user account for storing personal files.

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:~$

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 a and 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

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.

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]*

Options/Arguments

Options are used to modify the core behavior of a command while arguments are used to provide additional information (such as a filename or a username). Each option and argument is normally separated by a space, although options can often be combined together. Keep in mind that Linux is case sensitive. Commands, options, arguments, variables and filenames must be entered exactly as shown.

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:

Aliases

The ability to create short "nicknames" for longer commands.

Scripting

The ability to place commands in a file and execute the file, resulting in all of the commands being executed. This feature also has some programming features, such as conditional statements and the ability to create functions (AKA, subroutines).

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:~$

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/xml command 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:~$

Double Pipe (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".

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.

ls

The ls command will provide useful examples. By itself, the ls command will list the files and directories contained in your current working directory: An argument can also be passed to the ls command to specify which directory to list the contents of. For example, the command ls /etc/ppp will list the contents of the /etc/ppp directory instead of the current directory

relative pathname

The pathname of a target file or directory relative to your current directory in the tree.

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 t and 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:

Semicolon (Control Statement)

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:

Echo

To display the value of a variable, you can use the echo command. The echo command is used to display output in the terminal.

Variables

Variables are used to store information for the BASH shell. These variables can be used to modify how commands and features work as well as provide vital system information.

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:

cd

change directory

~ metacharacter

home directory

pwd

print working directory

parent directory

root of the tree


संबंधित स्टडी सेट्स

Mental Health: Ch. 18 Anxiety, Obsessive-Compulsive, and Related Disorders

View Set

English 047 Cumulative Speedback 2

View Set