Linux Shell Programming Summary

Source: Internet
Author: User

1. How Shell scripts work

The Linux shell script is similar to Windows batch, but it has a lot more powerful features than Windows batch processing. The shell script is actually a stack of shell commands, with syntax such as shell operations, conditional judgments, looping structures, and logical judgments, which enables the shell script program to implement powerful functions. So how does the shell script work? When it comes to shell scripting, let's talk about what the shell is. We can literally see that the shell is a shell, which is the interface that the operating system provides to the user's management operating system, similar to the Explorer (graphical interface) in Windows or the CMD Command prompt tool (Command interface Dos), the shell uses a command language, It is a command interpreter that interprets the commands entered by the user and then sends them to the kernel for processing. There are many different shell programs in Linux, the most common being bash (Bourne Again shell), which is an extension of the Bourne shell, and the first line of the bash script #! /bin/bash is meant to indicate the interpreter used by the script.

2. Variables
Variable can be changed, it is a piece of memory address space, used to store the data entered by the user or the operation of the program generated by the value or object

Variables in bash environment

Local variables:
Enter the set command in bash to see all locally defined shell variables scoped to the current shell process

[[email protected] ~]# Setbash=/bin/bashbashopts=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore : hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepathbash_aliases= () BASH_ARGC= () BASH_ argv= () bash_cmds= () bash_lineno= () bash_source= () bash_versinfo= ([0]= "4" [1]= "1" [2]= "2" [3]= "1" [4]= "Release" [5]=] X86_64-redhat-linux-gnu ") bash_version= ' 4.1.2 (1)-release ' colors=/etc/dir_colorscolumns=116dirstack= () EUID= 0groups= () g_broken_filenames=1histcontrol=ignoredupshistfile=/root/.bash_historyhistfilesize=1000histsize= 1000home=/roothostname=localhost.localdomainhosttype=x86_64ifs=$ ' \t\n ' lang=enlessopen= ' |/usr/bin/lesspipe.sh%s ' Lines=28logname=rootls_colors= ' rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;0 1:or=40;31;01:mi=01;05;37;41:su=37; Machtype=x86_64-redhat-linux-gnumail=/var/spool/mail/rootmailcheck=60opterr=1optind=1ostype=linux-gnupath=/usr /local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binpipestatus= ([0]="0") ppid=2867prompt_command= ' printf "\033]0;%[email protected]%s:%s\007" "${user}" "${hostname%%.*}" "${PWD/# $HOME/~} "' ps1= ' [\[email protected]\h \w]\$ ' ps2= ' > ' ps4= ' + ' Pwd=/rootselinux_level_requested=selinux_role_ Requested=selinux_use_current_range=shell=/bin/bashshellopts=braceexpand:emacs:hashall:histexpand:history: Interactive-comments:monitorshlvl=1ssh_client= ' 192.168.68.1 1116 ' ssh_connection= ' 192.168.68.1 1116 192.168.68.138 ' ssh_tty=/dev/pts/0term=xtermuid=0user=root_=_colors=/etc/dir_colorsftp_proxy=ftp:[email  protected]:3120
Environment variables:
Defines the environment information that the current shell runs, such as Path, PWD, home, and so on, scoped to the current shell process and its child processes, and environment variables can be viewed with env, printenv, export

Positional parameter variables:
Used in the script to refer to arguments passed to the script; in the function, refer to the arguments passed to the function, $n, n=0-9

Local variables:
Defined by the user in the program and used for the execution of the function

Special variables:
$? Status code for the last execution
The name of the script itself
$# Number of script parameters
The $* script parameter list, equivalent to "$ 1", passed a parameter
[Email protected] parameter list, equivalent to "$" "$" "$", equivalent to 3 parameters
$$ The process ID of the script run

#!/bin/bashecho $ #echo $0echo $*echo [email Protected]echo $ $for key in $*do   echo $keydonefor key2 in ' $* ' do   Echo $key 2donefor Key3 in [email protected]do  echo $key 3donefor key4 in "[email protected]" Do  echo $key 4done
Execution Result:
[[email protected] ~]# Bash values.sh 3 2 13values.sh3 2 2 13700----values in $* without quotation Marks-----321----val UEs in $* with quotation Marks-----3 2 1----values in [email protected] without quotation Marks-----321----values in [Emai L protected] with quotation Marks-----321

Assignment of variables:

Linux Shell Programming Summary

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.