# 使用场景 通过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