Linux 启动bash脚本
我需要自学bash脚本。我正在读这本电子书,它有以下代码:Linux 启动bash脚本,linux,bash,Linux,Bash,我需要自学bash脚本。我正在读这本电子书,它有以下代码: #!/bin/bash # hello.sh # This is my first shell script! declare -rx SCRIPT="hello.sh" declare -rx who="/usr/bin/who" declare -rx sync="/bin/sync" declare -rx wc="/usr/bin/wc" # sanity checks if test -z "$BASH" ; then
#!/bin/bash
# hello.sh
# This is my first shell script!
declare -rx SCRIPT="hello.sh"
declare -rx who="/usr/bin/who"
declare -rx sync="/bin/sync"
declare -rx wc="/usr/bin/wc"
# sanity checks
if test -z "$BASH" ; then
printf "$SCRIPT:$LINENO: please run this script with the BASH shell\n" >&2
exit 192
fi
if test ! -x "$who" ; then
printf "$SCRIPT:$LINENO: The command $who is not available - aborting\n" >&2
exit 192
fi
if test ! -x "$sync" ; then
printf "$SCRIPT:$LINENO: the command $sync is not available - aborting\n">&2
exit 192
fi
if test ! -x "$wc" ; then
printf "$SCRIPT:$LINENO: the command $wc is not available - aborting\n" >&2
exit 192
fi
USERS = `$who | $wc -l`
if [ $USERS -eq 0 ] ; then
$sync
fi
exit 0
当我运行它时,会出现以下错误:
hello.sh: line 32: USERS: command not found
hello.sh: line 33: [: -eq: unary operator expected
我真的不知道我做错了什么。我不允许以这种方式将用户分配到命令行的输出吗?如果我在命令行中运行该行,它也不会工作。有什么想法吗
谢谢更换
USERS = `$who | $wc -l`
与
替换
USERS = `$who | $wc -l`
与
删除赋值=”周围的空格:
或者,它将被解释为具有两个参数=和`%who |$wc-l`的命令用户删除赋值=:
或者它将被解释为一个命令用户,在Bash中有两个参数=和`%who |$wc-l`,事实上在许多shell中,变量名和符号之间不能有空格= 在这种情况下,您需要编写
USERS=`command`
或
变量有时充当C++宏。如果变量用户为空,则键入以下内容:
if [ $USERS -eq 0 ] ; then
它将被解释为
if [ -eq 0 ] ; then
并且-eq不是一元运算符。要使其正确,您需要写:
if [ "$USERS" -eq 0 ] ; then
解读
if [ "" -eq 0 ] ; then
在Bash中,实际上在许多shell中,变量名和符号之间不能有空格= 在这种情况下,您需要编写
USERS=`command`
或
变量有时充当C++宏。如果变量用户为空,则键入以下内容:
if [ $USERS -eq 0 ] ; then
它将被解释为
if [ -eq 0 ] ; then
并且-eq不是一元运算符。要使其正确,您需要写:
if [ "$USERS" -eq 0 ] ; then
解读
if [ "" -eq 0 ] ; then
因为它基本上没有编程语言那么复杂。实际上,除非你已经知道,否则你无法知道自己做错了什么阅读bash的手册页确实帮助了我学习,因为它简洁明了-不推荐使用反勾号。改用USERS=$$who |$wc-l。它可读性更好,不太依赖字体,可以嵌套而不会出现问题,因为它基本上没有编程语言那么复杂。实际上,除非你已经知道,否则你无法知道自己做错了什么阅读bash的手册页确实帮助了我学习,因为它简洁明了-不推荐使用反勾号。改用USERS=$$who |$wc-l。它可读性更好,不太依赖于字体,并且可以嵌套而不会出现问题。您有什么特定的原因必须学习bash吗?每次我从一个小的bash脚本开始,认为用Python做起来太简单了,后来我就把它转换成Python,这是一个质量很差的脚本。一个很好的资源是随和。我将查看该指南。谢谢实习期间我需要学习bash。我希望它只是python,但显然我将要使用的工具需要bash。你有什么特别的原因必须学习bash吗?每次我从一个小的bash脚本开始,认为用Python做起来太简单了,后来我就把它转换成Python,这是一个质量很差的脚本。一个很好的资源是随和。我将查看该指南。谢谢实习期间我需要学习bash。我希望它只是python,但显然我将要使用的工具需要bash。