Shell position variables in a detailed

Source: Internet
Author: User

What are positional variables
Calls the arguments passed to the script through the command line in the script code.
What are the positional variables
$1,$2,...: Corresponding to the 1th and 2nd parameters, Shift [n] position $: The command itself $*: All parameters passed to the script, all parameters are combined into a string [email protected]: All parameters passed to the script, each parameter is a separate string $#: The number of arguments passed to the script [email protected] $* is only a difference when wrapped in double quotes.
Positional variables Sample Script
1. $n gets the nth parameter of the current execution script, n=1..9,$0, as the current script name. If n is greater than 9, use ${10}
Echo ' Echo ' $ (seq-s "$" 1 5|sed ' s/1/$1/') > test_n.sh cattest_n.sh #内容如下 #echo $ $ $ $4 $ bashtest_n.sh arg1 agr2 Arg 3 #输出内容: #arg1 agr2 Arg3
2.$0 gets the file name of the current script
#脚本名称如下vim test1.sh #脚本内容如下 #!/bin/bashecho $ execute output as follows: test]# bash test1.shtest1.sh
3.$ #传递给脚本的参数的个数
The script reads as follows: #!/bin/bashecho $ #执行结果如下: ~]$ bash test3.sh a b C 1 25
[email protected] All parameters passed to the script, each parameter is a separate string; $*: All parameters passed to the script, all parameters are combined as a string [email protected] $* only when the double quotation marks are wrapped up the difference
Content in #脚本test2. Sh #!/bin/bashecho  ' this is a test for $* and [email  Protected] ' echo  ' the following start printing $* ' Echo $*./test4.sh $*echo   ' the following start printing [email protected] ' echo [email protected ]/test4.sh $*echo  ' the following start printing $# with  "" ' echo  " $* "./test4.sh " $* "echo  ' the following start printing [email protected]  with  "' echo " [email protected] "./test4.sh  " [email protected] "# Content in script test4.sh #!/bin/bashecho $ #执行结如下: [[Email protected] ~]$ ./test2.sh a b  c eThis is a test for $* and [email protected]  Following start printing $*a b c e4the following start printing  [email protected] b c e4the following start printing $# with  "" a b c e1The  following start printing [email protected] with  "" A b c e4
Shell exercises:
1, write script/root/bin/systeminfo.sh, display the current host system information, including host name, IPV4 address, operating system version, kernel version, CPU model, memory size, hard disk size.
#!/bin/baship= ' ifconfig | grep  ' bcast ' |tr -s  '   '  |cut -d  '   '  -f 3|cut -d: -f 2 ' osversion= ' cat /proc/cpuinfo |grep  "model name "|head -1|cut -d: -f2 ' cputype= ' uname -r ' memsize= ' free|sed -n  ' 2p ' |tr -s   '   ' |cut -d '   '  -f 2 ' disksize= ' fdisk -l |sed -n  ' 2p ' |cut  -d  '   '  -f 3,4 ' echo  "the system hostname is  ' hostname '" echo   "the system ip is  $IP" echo  "the system osversion is $ OsVersion "echo " the cpu type is  $CpuType "echo " the memory total  is  $MemSize "echo " the disk total is  $DiskSize "
2, scripting/root/bin/backup.sh, can be implemented daily/etc/directory backup to/root/etcyyyy-mm-dd
#!/bin/bashdir=/etc/backdir=/root/time= ' Date +%f ' echo ' Backup is start,please wait the alert of end ' backup= ' cp-a $Dir ${b Ackdir}/etc${time} ' echo ' All are finished '
3. Write script/root/bin/disk.sh, showing the maximum space utilization value in the current hard disk partition
#!/bin/bashmaxuse= ' Df-h|sed-nr ' [email protected]* (\b[[:d igit:]]+)%.*@\[email protected] ' |sort|tail-1 ' echo ' the Max Use of the disk is $MaxUse "
4. Write Script/root/bin/links.sh, show the IPV4 address and connection number of each remote host connecting to this host, and sort by the number of connections from large to small
#!/bin/bashecho "The link is ' netstat-nt|tr-s ' |cut-d" f 5|sed ' 1,2d ' |cut-d:-f1|sort |uniq-c|sort-nr ' "
5, write a script/root/bin/sumid.sh, calculate the/etc/passwd file of the 10th user and the 20th user ID of the sum
#!/bin/bashid10= ' cat/etc/passwd|sed-n ' 10p ' |cut-d:-F 3 ' id20= ' cat/etc/passwd|sed-n ' 20p ' |cut-d:-F 3 ' sumid=$ ((ID10+I D20) echo "Sum of the both UID is $SumId"
6, write a script/root/bin/sumspace.sh, pass two file paths as parameters to the script, calculate the sum of all the blank lines in these two files
#!/bin/bashspaceline1= ' cat $1|grep ' ^$ ' |wc-l ' spaceline2= ' cat $ |grep ' ^$ ' |wc-l ' let sumspace=${spaceline1}+${ Spaceline2}echo "Spcace line of the both file is $SumSpace"
7, write a script/root/bin/sumfile.sh, statistics/etc,/var,/usr directory total number of sub-directories and files
#!/bin/bashsum=$ (' ls/etc/|wc-l ' + ' ls/var/|wc-l ' + ' ls/usr/|wc-l ') echo "Sum file of the three dir is $Sum"
8, write a script/root/bin/argsnum.sh, accept a file path as a parameter, if the number of parameters is less than 1, then prompt the user "at least one parameter should be given" and immediately exit; If the number of arguments is not less than 1, the number of blank lines in the file pointed to by the first parameter is displayed 
#!/bin/bash[ $# -lt 1 ] && echo  "Please give at  Least one args " | |  echo  "' cat $1 |grep  ' ^$ '  |wc -l '  is the first args ' s  space line "
9, Chmod -rw /tmp/file1, script/root/bin/per.sh to determine if the current user is not readable to the/tmp/fiile1 file and is not writable 
#!/bin/bashfile1=/tmp/file1[ ! -r ${file1} -a ! -w ${file1} ]  && echo  "Current user can ' T read and write the file"  | | echo  "Current user can read and write the file" 
10, scripting/root/bin/nologin.sh and login.sh, implement prohibit and allow ordinary user login system. 
#/root/bin/nologin.sh#!/bin/bash[ -e /etc/nologin ] && echo  "The  Common user is already can ' T login '  && exittouch /etc/nologin  && echo  "Limit the common user ok" #login. sh#!/bin/bash[ !  -e /etc/nologin ] && echo  "the common user is  Already can login " && exitrm -rf /etc/nologin && echo   "The limit of common have remove" 
11, write a script/root/bin/hostping.sh, accept a host IPV4 address as a parameter, first determine whether the qualified IP, no, prompt IP format is not valid and exit, is, test whether can be connected. If Ping is available, the user is prompted to "This IP address is accessible", and if it is not ping, the user is prompted "The IP address is not accessible" 
#!/bin/bashipaddr= ' echo  ' |egrep -o  ' ([0-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \.) {3} ([0-9]| [1-9] [0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) "[ ! -n ipaddr  ] && echo   "Please give a legal ip address" &&exitping -c1 -w1 $1  &> /dev/null && echo  "The ip is access"  | | echo  "Cant ' t access the ip address" 
12, calculate the value of 1+2+3+...+100
#!/bin/bashsum=$ (' echo {1..100}|tr ' + ') ' echo $sum
13. Calculate the sum of all the numbers from the first parameter a of the script, to the second parameter B, to determine if B is greater than a, to indicate the error, and to exit.
#!/bin/bashf=$[$1-$2][$F-gt 0] && exitsum=$ (' seq $ |tr ' \ n ' ' |tr ' + ' |sed-r ' [email protected] (. *[^+ ]) \[email Protected]\[email protected]) echo $sum

This article is from "vinegar with soy sauce bottles" blog, please be sure to keep this source http://sauce.blog.51cto.com/11880696/1837835

Shell position variables in a detailed

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.