linux - 使用for循环在每个单词前后添加3个星号

标签 linux bash

我遇到无法完成此脚本的问题。这是有效的脚本的一部分。这也是一个 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/

相关文章:

linux - BASH:如何通过 ftp 传输文件名变量

bash - sh 命令 : exec 2>&1

linux - cron 调度程序如何 cd 到某个位置并执行 shell 脚本

linux - 命令行编辑 .CSV 文件

linux - Graphite 存储模式不起作用

ruby-on-rails - 未写入日志文件(乘客)

linux - 补丁:以非交互模式运行

mysql - BASH - 操作 URL

python - 将 Yocto 与默认使用 python3 的发行版一起使用

c - 使用 ARM 处理器 (HPS) 的 DE1-SoC 上的硬件加速算术逻辑单元 (ALU) Linux 应用程序