Bash 检查用户是否输入了三个文件名作为输入

Bash 检查用户是否输入了三个文件名作为输入,bash,Bash,我以前从未做过shell脚本,需要一些小项目的帮助 我希望用户输入三个文件名,并检查用户是否输入了三个文件名,如果输入的文件名或多或少,则给出一个错误。这些文件将被分类到另一个文件中,但这很好,只是检查用户输入的内容有问题 我试过了 echo Please select the three files you want to use read $file1 $file2 $file3 if ! [ $# -eq 3 ]; then echo "Please enter THREE val

我以前从未做过shell脚本,需要一些小项目的帮助

我希望用户输入三个文件名,并检查用户是否输入了三个文件名,如果输入的文件名或多或少,则给出一个错误。这些文件将被分类到另一个文件中,但这很好,只是检查用户输入的内容有问题

我试过了

echo Please select the three files you want to use
read $file1 $file2 $file3

if ! [ $# -eq 3 ]; then
   echo "Please enter THREE values"
fi 

在不更改
读取
命令的情况下:

if [ -z "$file1" -o -z "$file2" -o -z "$file3" ]; then echo "Please enter THREE values" fi
您做得很好,唯一的问题是在read语句中使用
$
。删除美元,您的代码将正常工作:

#!/bin/sh

echo "Please select the three files you want to use"
read file1 file2 file3

#Or something like:
#echo -n "File 1:"
#read file1
#echo -n "File 2:"
#read file2
#echo -n "File 3:"
#read file3

echo "File 1: $file1  File 2 : $file2  File 3 : $file3"

感谢老兄,这非常好用,也非常感谢你添加了元素,也节省了我查找这些元素的时间。我在选择答案的那一刻尝试了,但需要排名15,但仍然只有13,我会在我可以承诺的那一刻投票,刚刚获得排名并再次投票感谢老兄,你帮了大忙 for f in "${files[@]}"; do echo $f done
#!/bin/sh

echo "Please select the three files you want to use"
read file1 file2 file3

#Or something like:
#echo -n "File 1:"
#read file1
#echo -n "File 2:"
#read file2
#echo -n "File 3:"
#read file3

echo "File 1: $file1  File 2 : $file2  File 3 : $file3"