我需要自学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
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`
或者它将被解释为带有两个参数
=
和`%who$wc-l的命令USERS
。`关于linux - 启动bash脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10628778/