全球主机交流论坛

标题: [求助] shell for i in 循环执行 [打印本页]

作者: KDE    时间: 2023-2-9 14:48
标题: [求助] shell for i in 循环执行
本帖最后由 KDE 于 2023-2-9 14:54 编辑

for i in '1 3 5 7'
do
    echo "test $i"
done

输出:test 1 3 5 7

我想让输出变成如下,请问该怎么写?先谢谢大佬……
test 1
test 3
test 5
test 7

改成试试这样可以了
for i in 1 3 5 7
do
    echo test $i
done

但是下面这个带了单引号 结果不行了
sed -i '/$i/d' list.txt
作者: wnet    时间: 2023-2-9 14:57
本帖最后由 wnet 于 2023-2-9 14:59 编辑

#!/bin/bash
for i in {1,3,5,7};do printf "test $i\n";done

或者

#!/bin/bash
for i in {1,3,5,7};do echo "test $i";done

作者: Faxlok    时间: 2023-2-9 14:59
echo -e "test $1\n" 里面加个\n就可以换行了
作者: KDE    时间: 2023-2-9 16:11
wnet 发表于 2023-2-9 14:57
#!/bin/bash
for i in {1,3,5,7};do printf "test $i\n";done


大佬 我交下作业

# 创建测试文件
cat <<EOF> list.txt
www.google.com
www.google.co.jp
www.google.com.hk
dns.google
EOF

# 创建脚本
cat <<EOF> test.sh
for i in www.google.com www.google.co.jp www.google.com.hk
do
    sed -i "/\$i/"d list.txt
done
EOF

# 运行脚本
./test.sh

# 查看输出结果
[root@RHEL9 ~]# cat list.txt
dns.google






欢迎光临 全球主机交流论坛 (https://loc.010206.xyz/) Powered by Discuz! X3.4