什么$?和$#立场在Shell脚本? [英] What do $? and $# stand for in Shell Script?

查看:163
本文介绍了什么$?和$#立场在Shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code以下两个大块,我不能肯定什么$?和$#立场。请帮助。

I have the following two chunks of code, I am not so sure what $? and $# stand for. Please help.

code ONE

 #!/bin/bashUSERID="$1" 
 /bin/id $USERID 2>/dev/null
[ $? -eq 0 ] && echo "User found" || echo "User not found"

/bin/id -g $USERID 2>/dev/null
[ $? -eq 0 ] && echo "Group found" || echo "Group not found"    `


$ cat > mtable

code两个

#!/bin/sh
#
#Script to test for loop
#
#
if [ $# -eq 0 ]
then
echo "Error - Number missing form command line argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for given number"
exit 1
fi
n=$1
for i in 1 2 3 4 5 6 7 8 9 10
do
echo "$n * $i = `expr $i \* $n`"
done

感谢您

推荐答案

$?为您提供了previous命令的返回code执行。

$? gives you the return code of the previous command executed.

$#给你提供给脚本的命令行参数的个数。

$# give you the number of command line arguments given to the script.

所以,基本上,如果条件检查,如果给定的参数数分别为合适。

So, basically that if condition checks if the number of arguments given were proper or not.

这篇关于什么$?和$#立场在Shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆