Unix Re-learning--Shell programming __unix

Source: Internet
Author: User
Tags builtin chmod echo command gpg file permissions

UNIX Environment Advanced Programming read Chapter Three, encountered many redirects and other shell commands. When I think about Linux, it seems necessary to go ahead. Have seen a "embedded Linux software and hardware development detailed" This book has a brief introduction of a part of the shell commonly used commands, it is combined to introduce a simple shell programming. After all, did not have a detailed look at the shell-related books, and so on later read the details.

First, we recommend a shell online tool

Extended Learning: Shell Tutorials

Related books: Linux command line and Shell script programming encyclopedia

Common shortcut keys:

CTRL + C force the termination of the current command  
ctrl+l screen, equivalent  
to clear ctrl+u Delete or cut all the commands before the cursor, faster than the backspace  
ctrl+k Delete or cut all the commands after the cursor  
ctrl+y Paste Ctrl+u or ctrl+k cut content  
ctrl+r implement search History command, enter Ctrl+r First, then enter the history command that needs to search  
Ctrl+d exit the current terminal  
ctrl+z suspend command and put in the background, You cannot use  
ctrl+s to pause screen output frequently  

One, bash shell introduction 1, in the UNIX Basics chapter, a brief introduction to the shell: Shell is a command-line interpreter that reads user input and then executes the command . Shell user input typically comes from a terminal (interactive shell) , and sometimes from a file (called a shell script) .
Common Shell in UNIX systems: Bourne Shell   path:/bin/sh 
Bourne-again Shell   Path:/bin/bash
C Shell   path:/bin/csh 
Korn Shell   path:/bin/ksh 
Tenex C Shell   path:/BIN/TCSH
where the & nbsp The bourne-again Shell is the bash shell we're talking about, it's the GNU shell, and all Linux systems provide this shell. its design follows the POSIX standard while preserving compatibility with the Bourne shell. It supports both the C shell and Korn shell features. The system understands which shell should be executed for the logged-on user from the corresponding last field of user login in the password file. I use the virtual machine is Ubuntu 12.04, the kernel is a Linux 3.2.0, using the root super user. View User Login file/etc/passwd 

View/etc/passwd:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin :/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/ Usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
Can see: Root:x:0:0:root:/root:/bin/bash So, what I'm doing is Bash ShellOther field meaning, you can See : Unix Re-learning--Re-recognition 2, compared with other shellsBash shells are standard shells in Linux operating systems, and almost all Linux versions currently use the Bash shell as the core of system management, and bash shells are more powerful than other shells: (1) command memory functionWe can view the instructions that were used before by pressing the "up and down" key on the keyboard. After each login to execute the instructions are temporarily in the buffer, the successful exit system, the instructions will be recorded in the Bash_history file. With this feature, we can easily modify the wrong execution commands. (2) command and file completion functionWith this feature, we can do a lot less typing and make sure that the data entered is correct. "Tab" is followed by the first word of a string of commands, which is "complete" for "command completion", and "tab" is followed by the second word of a string of commands. With this feature, we can quickly view or match the relevant commands or files in the current directory. (3) Command alias setting functionLinux systems contain a variety of command names and parameters that are neither convenient nor easy to manage. The bash shell provides a custom command using the Alias aliasThe function. (4) Programming functionThe shell can be used not only as a command interpreter to customize the working environment, but also as a high-level programming language for executing user directives ScriptTo deal with complex tasks more quickly and efficiently. Second, bash shell common commandsWhen User Login to Linux system, it starts interacting with bash until the user logs off (this section is described in detail later on when Linux starts). If it is a normal user, bash the The default prompt is "$" (on behalf of ordinary users), if it is Root Superuser, the prompt becomes "#"。 The process by which the user interacts with the system is done by entering the action command after the prompt. To enhance the Shell's processing power, the bash shell, in addition to its built-in commands, such as CDs, also adds support for external application commands, such as LS, PS, and so on. The command entered after the Shell's command prompt has its own response if it is a built-in command for the bash shell; if it is an external application command, the shell finds the corresponding external application and then gives control to the kernel, which is executed by the kernel and then the control is returned to Shell common commands are as follows:This part is more content, search: Linux command Daquan Shell build command 1, type command format:Type parameter command function:Determine whether a command is a built-in command or an external command Options:
-T: Output "file", "Alias", or "builtin", respectively, indicating that the given instruction is "external instruction", "command alias", or "internal instruction";-
P: Displays its absolute path if the given instruction is an external instruction; 
-A: In the environment variable " Path, displays information about the given instruction, including the command alias.
Example:
# type LS
ls is the alias of ' ls--color=auto '

# type-t ls
alias

# type-a ls
ls is an alias of ' LS--color=auto ' 
  
   ls is/bin/ls

can see that LS is the command alias
  
# Type cd
CD is Shell inline

# type-t CD
builtin

# type-a CD
CD is Shell inline

can see that CD for internal command
# type which
which is/usr/bin/which

# type-t which
file

# type-p which

# t Ype-a which
which is/usr/bin/which
which is/bin/which

can be seen which for external command
With the use of the type command, we can tell if each command is a bash built-in command. In addition, when you use type to search for a name that follows, the name does not appear if the subsequent name cannot be found in the state in which the file was executed. 2, Echo command format:Echo Arg function:Displays a string with arg specified on the screen Example:
Simple display:
# echo Hello
Hello
Create shell script:
gedit hello.sh
#!/bin/bash
echo "Hello world!"

Execute./hello.sh
bash:./hello.sh: Insufficient permissions
Add permissions: chmod +x *.sh  or  chmod 777 *.sh

#./hello.sh 
Hello world!
Extended section: Echo command
Options: - E: Activates the escape character.
When the-e option is used, the following characters appear in the string, and are handled in particular, rather than as normal text output:
The \a emits a warning sound; 
\b Deletes the previous character; the 
\c ends with a newline symbol; 
\f Wraps but the cursor remains in its original position; \ n Wraps and the cursor moves to the beginning of the line 
; 
\ r cursor moves to the beginning of the line, but does not wrap;
\ t Insert tab; \v is the same as \f; \ 
insert \ character; 
\nnn Insert the ASCII character represented by NNN (octal);
Example:
# echo-e ' \e[1;31mthis is red text\e[0m ' This is
red text

# echo-e ' \e[1;42mgreed background\e[0m ' greed
Bac Kground

3, ExportThis part speaks of environmental variables, See : Unix Re-learning-environment variables command format 1:Export variable function:The shell can use export to bring its variables down into the child shell, allowing the child process to inherit the environment variables in the parent process. But the child shell cannot use export to bring its variables up into the parent process. Example:
# export hello= "HELLO"  
# echo $HELLO  
Command Format 2:Export function:Displays all current environment variables and their contents. Example:
# Export
declare-x colorterm= "gnome-terminal"
declare-x dbus_session_bus_address= "unix:abstract=/tmp/ dbus-kss6b0aqua,guid=297ad74aed4e17b7f89f981d0000003c "
declare-x defaults_path="/usr/share/gconf/ Ubuntu-2d.default.path "
declare-x desktop_session=" ubuntu-2d "Declare-x display=
": 0 "
Declare-x gdmsession= "ubuntu-2d"
declare-x gnome_desktop_session_id= "this-is-deprecated" Declare-x GNOME_KEYRING_
Control= "/tmp/keyring-qyaxfa"
declare-x gpg_agent_info= "/tmp/keyring-qyaxfa/gpg:0:1" Declare-x GTK_IM_
Module= "IBUs"
declare-x home= "/root"
...
4, ReadOnly command format 1:ReadOnly variable feature: Identifies a user-defined shell variable as mutable Example:
# export hello= "Hello"  
# readonly hello= "Hello"  
# export hello= "Hello" World  
Bash:hello: Read-only variable  
# unset HELLO  
Bash:unset:HELLO: cannot be reversed: read-only variable  
Command Format 2:ReadOnly function:Show all read-only shell variables Example:
# readonly
declare-r bashopts= ' checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend: Hostcomplete:interactive_comments:progcomp:promptvars:sourcepath "
declare-ir bashpid
declare-ar bash_ Versinfo= ' ([0]= "4" [1]= "2" [2]=] [3]= "1" [4]= "Release" [5]= "I686-pc-linux-gnu") '
declare-ir euid= ' 0
] Declare-ir ppid= "2590
declare-r shellopts=" Braceexpand:emacs:hashall:histexpand:history: Interactive-comments:monitor "
declare-ir uid=" 0 "
5, Read command format:Read variable function:Reads a line from a standard input device, decomposes it into rows, and assigns values to the variables defined by the shell program. Example:
Create shell script:
gedit hello.sh
#!/bin/bash
echo-e "Please enter: \c"
read x
echo "You enter: $x"

Execute script:
#./hello.sh please 
Enter:hello world!
You Enter:hello world!
For example, when a terminal enters a password and does not allow the password to appear, the following script can be created:
Method One:
Create script:
#!/bin/bash 
read-p "Enter password:"-s pwd 
echo echo password read, is "$pwd"

Execute script:
./ hello.sh
Enter password:
password read, is 12345
Method Two:
create a script:
#!/bin/bash 
stty-echo
read-p "Enter password:" pwd
stty echo echo
input Complete

Execute script:
./hello.sh
Enter Password:
input complete
Note: The Stty-echo option-echo prevents the output from being sent to the terminal, while option echo allows the output to be sent.
6, Env command format:Env function:Displays environment variables and their contents. Example:
# env
Lc_paper=en_us. UTF-8
Lc_address=en_us. UTF-8
ssh_agent_pid=1749
lc_monetary=en_us. UTF-8
gpg_agent_info=/tmp/keyring-qyaxfa/gpg:0:1
term=xterm
shell=/bin/bash
...
7, set command format:Set function:Show all variables and their contents Example:
# set
bash=/bin/bash
bashopts=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend: Hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
bash_aliases= ()
bash_argc= (
) bash_argv= ()
bash_cmds= ()
bash_lineno= ()
bash_source=
() .....
8, unset command format:unset function:Deletes a variable or function from the environment. This command cannot delete a read-only variable defined by the shell itself. Example:
# export hello= "Hello" #  
unset Hello  
# echo $HELLO  
  
#  
9, grep command format:grep parameter string destination file function:Finds a specific string in a heap of files in the specified file and outputs the string's row to the terminal or platform. See : grep command Options:
-A Do not ignore binary data. 
-a< shows the number of columns > displays the contents of the row except for the line that conforms to the template style. -B Displays the 
contents of the row before the line that conforms to the template style. - 
C calculates the number of columns that conform to the template style. 
-c< Displays the number of columns > or-< the number of columns > displays the contents of the column, except for the one that matches the template style. 
-d< Action > When you specify that you want to find a directory other than a file, you must use this parameter, otherwise the grep command returns information and stops the action. 
-e< Template Style > Specify a string as a template for finding the contents of a file. - 
e uses a generic notation that extends the template style, meaning that you can use an extended regular expression. The 
-f< template file > Specifies a template file with one or more template styles that lets grep find the contents of the file that match the template criteria, in the form of a template style for each column. -F treats the 
template style as a list of fixed strings. 
g uses the template style as a normal representation. - 
h does not indicate the name of the file to which the column belongs before displaying the column that conforms to the template style. - 
h indicates the file name of the column before displaying the column that conforms to the template style. I- 
hu column character case difference. - 
L lists the file names for which the contents of the file conform to the specified template style. 
-L lists the file names for which the contents of the file do not conform to the specified template style. - 
n marks the column number before displaying the column that conforms to the template style. 
-Q does not display any information. 
-r/-r The effect of this parameter is the same as the specified "-D recurse" argument. 
-S does not display an error message. 
-V Reverse lookup. 
-W Displays only the columns that match the whole word. 
-X displays only the columns that are eligible for the column. 
-y This parameter effect is the same as "-I". - 
o outputs only the parts of the file that match.
Common methods:
All current files with Hello
* grep "Hello" *-rn 

all current files that do not contain hello 
10. WC command format:WC parameter file 1 file 2 .... function:Counts the number of bytes in the specified file, the number of words, lines, and displays the results for the output. Options:
-C or--bytes or--chars: Displays only the number of bytes,- 
L or--lines: Only the number of columns,- 
W or--words: Displays only the word count.
Example:
# WC aio.h 
 246  967 7502 aio.h
are: column number of  words  byte number of file name
third, redirect and pipelineWhen executed, the shell command automatically opens three standard files, standard input files (stdin, general corresponding terminals), standard output files (STDOUT) and standard error output files (stderr, corresponding terminal screens), See : C language Re-learning-documentsIn practical applications, these three files often need to be directed in a new format. import content from other files or export content to other files, this process is redirectmake content output in a certain format, this is Piping1. redirect Redirection can be divided into output redirection, error redirection and input redirection (1) Output redirectPass redirection character ">" or ">>"Redirects the standard output of a command to a specified file. general form:Both command > filename ">" and ">>" can be written back to the file. However, if there is content in the file, the new content will overwrite the original content when the ">" is executed, and ">>" will append the new output to the end of the original content. Example:
New Text.txt
# touch text.txt

output PS content to text.txt file
# PS > text.txt 

view text.txt content
# cat Text.txt
  pid TTY time          CMD
 2600 pts/0    00:00:00 bash
 3231 pts/0 00:00:00    PS

if the LS content is then output by using ">" to text.txt file
# ls > text.txt 
to view text.txt content, then overwrite the original content
# cat Text.txt 
text.txt

If you use the PS content again ">>" output to Text.txt file
# PS >> text.txt 
and then view text.txt content, append to the end of the original content
# cat Text.txt 
Text.txt
  PID TTY time          CMD
 2600 pts/0    00:00:00 bash
 3236    pts/0 00:00:00 PS
(2) Error redirectionPass redirection character "2>" or "2>>"Redirects the standard error output of the command to the specified file. "2>" and "2>>" difference with the above ">" and ">>" here is not explained. general form:Command 2> file name command 2>> filename Example:
Hello do not have this file, view will be wrong
# cat Hello
cat:hello: No that file or directory to

save the error to the Text.txt file
# cat Hello 2> text.txt 
# Cat Text.txt 
Cat:hello: no file or directory
(3) input redirectionPass redirection character "<"Relocate the standard input of the command to the specified file. general form:Command < filename Example:
View Script
# cat sh.sh 
echo "You working directory is $ (PWD)" "echo" "The" "The" and "
$ (date)"

Shell command resolver from script sh Read the command line in SH and execute
# Bash < sh.sh 
' you working directory Is/home/tarena/project/c_test ' The time is
Wed Ma R-10:07:45 CST 2017
2, PipelineUnder Linux We can use the pipe operator "|" To connect multiple commands or processes, and each command is an independent process on both sides of the attached pipe line. The output from the previous command is the input to the next command. These processes can be done at the same time, and can be performed with more complex tasks as the data flow is automatically coordinated between them. We are not unfamiliar with the pipeline, which is useful before speaking xargs usage. See : C language Re-learning-xargs usage detailed general form:[Command 1] | [Command 2] | [Command 3] Example:
ls command view
# ls
sh.sh  text.txt

can specify find script file
# ls | grep *sh
sh.sh
Four, Shell simple applicationIn addition to being used as a command compiler for administrative commands, the shell can also used for programming .。 It provides a means to define variables and parameters, as well as a rich process control structure. Using shell programming is similar to using a batch file in DOS, called Shell Script, also called a shell program or a shell command file. 1. Basic Usage (1) BeginningThe program must start with the following line and must be placed on the first line of the file.
#。 /bin/bash
symbol "#!"To tell the system that the parameters behind it are the programs that are used to execute the file, in this example using the /bin/bashTo execute the program. And/bin/bash is the path to the bash shell. When the script is compiled, we must also make it executable if the script is to be executed. To make the script executable, we need to give the file the permissions it can execute, using the following command file:
chmod +x [file]  or  chmod 777 [file]
Modify file permissions before speaking to See : C Language Learning--modifying Linux file Permissions
(2) NoteWhen you are doing shell programming, A sentence that begins with "#" represents a comment until the end of the line, we recommend that you use annotations in your programs. With annotations, even if the script is not used for a long time, we can understand how the script works in a very short time. (3) PromptUsing in Scripts prompt "$", followed by parenthesesEncloses the command, you can execute the command. If it is Environment Variables, it follows "$" directly. Example:
View Script
# cat sh.sh 
#!/bin/bash
echo $ (LS)
echo $SHELL

execute Script
#./sh.sh 
sh.sh Text.txt
/bin/bash
2. Script Demo (1) Create a script
#!/bin/bash
#program Date
#show The date in this way
echo "Mr. $USER, ' is: '
#echo $ (date)
echo $ (date)
echo wish a lucky day!	
(2) Set executable permissions
# chmod 777 Sh.sh 
(3) Implementation procedures
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.