我遇到无法完成此脚本的问题。这是有效的脚本的一部分。这也是一个 linux bash 脚本。
echo -n "Please enter file "
read file
if [ -f $file ]
then
echo "$file found."
else
echo "$file not found."
fi
使用“for”循环编写一个脚本,打印文件中的每个单词,每个单词前后都有 3 个星号 (*)。该脚本应提示用户输入文件名,并且必须验证该文件是否存在以及该文件是否为常规文本文件。
提示:将 cat、read 和 file 命令以及 if 语句与 for
循环结合使用。
最佳答案
你可以使用这个脚本:
echo -n "Please enter file "
read file
if [ -f $file ]
then
echo "$file found."
if [[ $(file -b $file) == 'ASCII text' ]]
then
for word in $(cat $file)
do echo "***$word***"
done
else
echo "$file is not a regular text file"
fi
else
echo "$file not found."
fi
使用这个输入文件:
word1 word2
word3
这个脚本给出:
Please enter file test.inp
test.inp found.
***word1***
***word2***
***word3***
关于linux - 使用for循环在每个单词前后添加3个星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12559648/