首页 > 编程知识 正文

shell中for循环拼接字符串

时间:2023-05-05 02:03:25 阅读:183914 作者:1430

# 使用场景 通过shell脚本传参指定表名、分区字段和普通字段导入数据到hive表# sh test.sh a b c d e f# 输出 d,e,f# 在当前目录下创建文本文件temp,如果文件存在则清空文件$(> temp)# for循环将参数追加到当前目录的temp文件,逗号分隔,echo -n 不换行for i in $*;do((n++))# 从第四个开始拼接if [[ n -gt 3 ]];thenecho -n ${i}, >> tempfidone# str取temp文本里的字符串str=$(cat temp)# 将字符串最后的一个逗号去掉str=${str%*,}echo $str # 读文件内容到数组中# ip.txtaddress: 10.157.30.151address: 10.157.30.152address: 10.157.30.153# 方法一n=0;while read a b;do array[$n]=$b;((n++));done<ip.txtecho ${array[*]}# 方法二arr=($(awk '{print $2}' ip.txt))echo ${arr[*]}echo ${arr[@]}# 方法三 遍历for x in `awk '{print $2}' ip.txt`{ echo $x}# 方法四n=1while ((n<=$(cat ip.txt|wc -l)))do ip[$n]=$(cat ip.txt|sed -n "${n}p"|awk '{print $2}') ((n+=1))doneecho ${ip[*]}# 10.157.30.151 10.157.30.152 10.157.30.153

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。