[英]programming in linux bash, my program does not respond
我在 linux bash 工作,我有一个问题,我在做一个练习,说的是:“将通过键盘询问三个字符串,并测试是否输入了三个有效文本或这些字符串是否对应于目录名称。如果是,列出每一个里面的内容并显示出来。如果没有输入三个字符串或三个中的任何一个不是目录,则显示一个警告,指示错误。 好的,在我的代码中,我询问字符串,然后,正如您在每个 if 中看到的那样,我正在比较它们是否对应于目录名称。 但是,bash 给我一个在线错误。 option_4.sh:第 52 行:语法错误:文件结尾不是预期的。
#!/bin/bash
echo "Ask for three string from keyboard";
read -p "Give me a string:" string1;
read -p "Give me another string:" string2;
read -p "Give another string:" string3;
if [ -d "$string1" ];
cd "$directorio"
if [ -d "$string1" ];
then
echo ""
echo "The cadena1 is the name of a directory.."
echo "His content ($string1):"
ls ./"$string1"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
if [ -d "$string2" ];
cd "$directorio"
if [ -d "$string2" ];
then
echo ""
echo "The cadena2 is the name of a directory."
echo "His content ($string2):"
ls ./"$string2"
else
echo "ERROR: The cadena2 does not correspond to the name of a directory."
fi
if [ -d "$string3" ];
cd "$directorio"
if [ -d "$string3" ];
then
echo ""
echo "The cadena3 is the name of a directory."
echo "His content ($string3):"
ls ./"$string3"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
if 语句的语法如下所示:
if [ <some test> ]
then
<commands>
else
<commands>
fi
您需要将每个字符串的两个 if 语句中的第一个更改为
if [ -d "$string1" ];
then
cd "$directorio";
fi
甚至将这两个检查结合起来,因为下一个检查具有相同的条件:
if [ -d "$string1" ];
then
cd "$directorio" # moved here
echo ""
echo "The cadena1 is the name of a directory.."
echo "His content ($string1):"
ls ./"$string1"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
只是选择一个片段来描述它,我不确定您是要使用绝对路径还是相对路径。 所以在你的脚本中使用pushd
和popd
会更方便。
如果要在脚本中添加条件检查,则需要在关键字if
之后添加then
,最后需要添加关键字fi
if [ -d "$string3" ];
cd "$directorio"
if [ -d "$string3" ]; then
pushd "$directorio"
echo ""
echo "The cadena3 is the name of a directory."
echo "His content ($string3):"
ls ./"$string3"
popd
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.