shell中for循环拼接字符串

25 篇文章 0 订阅
# 使用场景 通过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 ]];then
		echo -n ${i}, >> temp
	fi
done
# str取temp文本里的字符串
str=$(cat temp)
# 将字符串最后的一个逗号去掉
str=${str%*,}
echo $str
# 读文件内容到数组中
# ip.txt
address:  10.157.30.151
address:  10.157.30.152
address:  10.157.30.153
# 方法一
n=0;
while read a b;do
    array[$n]=$b;((n++));
done<ip.txt
echo ${array[*]}

# 方法二
arr=($(awk '{print $2}' ip.txt))
echo ${arr[*]}
echo ${arr[@]}

# 方法三 遍历
for x in `awk '{print $2}' ip.txt`
{
    echo $x
}

# 方法四
n=1
while ((n<=$(cat ip.txt|wc -l)))
do
    ip[$n]=$(cat ip.txt|sed -n "${n}p"|awk '{print $2}')
    ((n+=1))
done
echo ${ip[*]}

# 10.157.30.151 10.157.30.152 10.157.30.153

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值