shell_36.Linux处理循环的输出

news/2024/7/24 11:31:33 标签: linux, 运维, 服务器

处理循环的输出
1.在 shell 脚本中,可以对循环的输出使用管道或进行重定向。
这可以通过在 done 命令之后添加一个处理命令来实现:

for file in /home/rich/* 
do 
    if [ -d "$file" ] 
    then 
        echo "$file is a directory" 
    elif 
        echo "$file is a file" 
    fi 
done > output.txt


shell 会将 for 命令的结果重定向至文件 output.txt,而不再显示在屏幕上。

2.考虑下面这个将 for 命令的输出重定向至文件的例子:

$ cat test23 
#!/bin/bash 
# redirecting the for output to a file 
for (( a = 1; a < 10; a++ )) 
do 
    echo "The number is $a"
done > test23.txt 
echo "The command is finished." 
$ ./test23 
The command is finished. 
$ cat test23.txt 
The number is 1 
The number is 2 
The number is 3 
The number is 4 
The number is 5 
The number is 6 
The number is 7 
The number is 8 
The number is 9 
$

3.也可以用于将循环的结果传输到另一个命令:

$ cat test24 
#!/bin/bash 
# piping a loop to another command 
for state in "North Dakota" Connecticut Illinois Alabama Tennessee 
do 
    echo "$state is the next place to go" 
done | sort 
echo "This completes our travels" 
$ ./test24 
Alabama is the next place to go 
Connecticut is the next place to go 
Illinois is the next place to go 
North Dakota is the next place to go 
Tennessee is the next place to go 
This completes our travels 
$


实战1:查找可执行文件

$ cat test25 
#!/bin/bash 
# finding files in the PATH 
IFS=: 
for folder in $PATH 
do 
    echo "$folder:" 
    for file in $folder/* 
    do 
        if [ -x $file ] 
        then 
            echo " $file" 
        fi 
    done 
done 
$


实战2: 创建多个用户账户

$ cat test26 
#!/bin/bash 
# process new user accounts 
input="users.csv"                 #####创建一个文本文件,内容包含(loginname, name)
while IFS=',' read -r loginname name    ######将 IFS 分隔符设置成逗号,并将其作为 while 语句的条件测试部分。然后使用 read 命令读取文件中的各行。
do 
    echo "adding $loginname" 
    useradd -c "$name" -m $loginname 
done < "$input" 
$


http://www.niftyadmin.cn/n/5112899.html

相关文章

大规模语言模型人类反馈对齐--PPO算法代码实践

在前面的章节我们已经知道&#xff0c;人类反馈强化学习机制主要包括策略模型、奖励模型、评论模型以及参考模型等部分。需要考 虑奖励模型设计、环境交互以及代理训练的挑战&#xff0c; 同时叠加大语言模型的高昂的试错成本。对于研究人员来说&#xff0c; 使用人类反馈强化学…

详细介绍如何使用Ipopt非线性求解器求解带约束的最优化问题

本文中将详细介绍如何使用Ipopt非线性求解器求解带约束的最优化问题&#xff0c;结合给出的带约束的最优化问题示例&#xff0c;给出相应的完整的C程序&#xff0c;并给出详细的解释和注释&#xff0c;以及编译规则等 一、Ipopt库的安装和测试 本部分内容在之前的文章《Ubuntu2…

42914-2023 铝合金产品断裂韧度试验方法

1 范围 本文件描述了铝合金产品断裂韧度的试验方法。 本文件适用于铝合金轧制板材、挤压棒材、挤压板材、挤压管材、挤压型材和锻件产品的平面应变断 裂韧度和平面应力断裂韧度的测定。 2 规范性引用文件 下列文件中的内容通过文中的规范性引用而构成本文件必不可少的条款…

openCV的CUDA GPU 版本安装 (Ubuntu windows 通用)

需要做template match, 比较注重时间&#xff0c;因此opencv 的普通版本不适用。需要用GPU 的。 下载 git clone https://github.com/opencv/opencv.git git clone https://github.com/opencv/opencv_contrib.git确保准备好以下内容 1&#xff1a; visual studio &#xff0…

FL Studio21中文版本好用吗?值不值得下载

今天&#xff0c;我从一个FL Studio忠实且还算资深的用户角度&#xff0c;来为大家深度介绍并评测一下FL Studio的性能以及我四年的使用感受。 FL Studio是一款集剪辑、编曲、录音、混音一体的全能DAW&#xff08;数字音频工作站&#xff09;。其所有界面都是支持100%矢量化的…

存储优化知识复习二详细版解析

存储优化 知识复习二 一、 选择题 1、 对数据库调优的方法中&#xff0c;最困难但是最有成效的是( )。 A、优化表的架构设计 B、添加内存 C、索引优化 D、查询语句优化 【参考答案】A2、 防止与处理死锁的方法有&#xff08; &#xff09;。 A、尽量避免或尽快处理阻塞 B、访…

Vue3中getCurrentInstance()方法详解

proxy只是一个变量名&#xff0c;翻译过来是“代理”的意思 当你使用 const { proxy } getCurrentInstance() 这句代码时&#xff0c;它执行了以下步骤&#xff1a; getCurrentInstance() 是 Vue 3 中的一个函数&#xff0c;用于获取当前正在执行的 Vue 组件实例的上下文信息…

十四天学会C++之第八天:文件操作

1. 文件的打开和关闭 文件操作的基本概念。打开文件&#xff1a;使用fstream库打开文件以供读写。关闭文件&#xff1a;确保文件在使用完毕后正确关闭。 文件的打开和关闭&#xff1a;C 文件操作入门 在C编程中&#xff0c;文件操作是一项重要的任务&#xff0c;可以读取和写…