我参加了一门Linux课程,我们讨论的是bash脚本。以下脚本应该使用字符串值打印回显语句,但不会。
#/bin/bash
echo "Enter the first string"
read str1
echo "Enter the second string"
read str2
echo $str1
echo $str2
myLen1=${#str1}
myLen2=${#str2}
if [ ! -z $str1 ]; then
echo Length of the first string is: $myLen1
else
echo Please enter a value for ${str1} with more than 0 characters
fi
if [ ! -z $str2 ]; then
echo Length of the second string is: $myLen2
else
echo Please enter a value for $str2 with more than 0 characters
fi
我曾尝试没有成功如下:
echo Please enter a value for ${str2} with more than 0 characters
echo Please enter a value for "$str2" with more than 0 characters
echo "Please enter a value for $str2 with more than 0 characters"
echo "Please enter a value for ${str2} with more than 0 characters"
任何想法?
该字符串有0个字符。你打算什么时候看到它? –
@EtanReisner这很有道理。显然,没有东西会打印。我很抱歉。相反,我只是附加它们来说'回声'条目1:...“'和'回声”条目2:...“' – Debug255
添加'set -vx'来启用shell调试/跟踪功能。然后你可以在执行代码之前看到每一行代码,然后该行以'+'开始,并且所有变量替换都已就绪。然后你就可以理解你的代码正在发生什么。顺便说一句,'set -v'部分实际上处理代码块,所以它会打印出一个巨大的while循环,然后只有'+'行被实际执行。它可能会令人困惑,究竟哪一行正在执行。添加'export PS3 ='$ LINENO>''来查看正在执行的代码的行数。祝你好运。 – shellter