Unix script 10 - variables II 输入变量

There are a set of variables which are set for you already, and most ofthese cannot have values assigned to them.
These can contain useful information, which can be used by the scriptto know about the environment in which it is running.

The first set of variables we will look at are $0 .. $9 and $#.
The variable $0 is the basename of the program as itwas called.
$1 .. $9 are the first 9 additional parametersthe script was called with.
The variable $@ is all parameters$1 .. whatever. The variable$*, is similar, but does not preserve any whitespace, and quoting, so "File with spaces" becomes "File" "with" "spaces". This is similar totheecho stuff we looked at inA First Script.As a general rule, use$@ and avoid$*.
$# is the number of parametersthe script was called with.
Let's take an example script:


var3.sh
#!/bin/sh
echo "I was called with $# parameters"
echo "My name is $0"
echo "My first parameter is $1"
echo "My second parameter is $2"
echo "All parameters are $@"

Let's look at running this code and see the output:


$ /home/steve/var3.sh
I was called with 0 parameters
My name is /home/steve/var3.sh
My first parameter is
My second parameter is    
All parameters are 
$
$ ./var3.sh hello world earth
I was called with 3 parameters
My name is ./var3.sh
My first parameter is hello
My second parameter is world
All parameters are hello world earth
Note that the value of $0 changes depending on how thescript was called. The external utility basename can help tidythis up:
echo "My name is `basename $0`"
$# and $1 .. $2 are setautomatically by the shell.
We can take more than 9 parameters by using the shiftcommand; look at the script below:
var4.sh
#!/bin/sh
while [ "$#" -gt "0" ]
do
  echo "\$1 is $1"
  shift
done              

This script keeps on using shift until $# is downto zero, at which point the list is empty.

Another special variable is $?. This contains the exit valueof the last run command. So the code:


#!/bin/sh
/usr/local/bin/my-command
if [ "$?" -ne "0" ]; then
  echo "Sorry, we had a problem there!"
fi

will attempt to run /usr/local/bin/my-command which shouldexit with a value of zero if all went well, or a nonzero value on failure.We can then handle this bychecking the value of$? aftercalling the command. This helps make scripts robust and more intelligent.
Well-behaved applications should return zero on success. Hence the quote:

One of the main causes of the fall of the Roman Empirewas that, lacking zero, they had no way to indicatesuccessful termination of their C Programs. (Robert Firth)

The other two main variables set for you by the environment are $$and$!. These are both process numbers.
The $$variable is the PID (Process IDentifier) of the currently running shell.This can be useful for creating temporary files, such as/tmp/my-script.$$ which is useful if many instances ofthe script could be run at the same time, and they all need their own temporary files.
The $! variable is the PID of the last run background process.This is useful to keep track of the process as it gets on with its job.

Another interesting variable is IFS. This is the InternalField Separator. The default value isSPACE TAB NEWLINE,but if you are changing it, it's easier to take a copy, as shown:


var5.sh
#!/bin/sh
old_IFS="$IFS"
IFS=:
echo "Please input three data separated by colons ..."
read x y z
IFS=$old_IFS
echo "x is $x y is $y z is $z"

This script runs like this:

$ ./ifs.sh
Please input some data separated by colons ...
hello:how are you:today
x is hello y is how are you z is today

It is important when dealing with IFS in particular (but any variable not entirely under your control) to realisethat it could contain spaces, newlines and other "uncontrollable" characters. It is therefore a very good ideato use double-quotes around it, ie: old_IFS="$IFS" instead of old_IFS=$IFS.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值