Bash~修改要打印的for循环

Bash~修改要打印的for循环,bash,for-loop,awk,printf,Bash,For Loop,Awk,Printf,好的,我的bash脚本中有这一行,如下所示: basic_list=$(awk '{printf "%s/%s/%s/%s\n", $1, $2, $3, $4}' <<< "${STANDARD_LIST}") basic_list=$(awk'{printf”%s/%s/%s/%s\n“,$1,$2,$3,$4}'如果需要设置bash变量,请不要使用awk。直接在bash中工作,使用类似 declare -a tmp tmp2 tmp3 tmp4 while read -

好的,我的bash脚本中有这一行,如下所示:

basic_list=$(awk '{printf "%s/%s/%s/%s\n", $1, $2, $3, $4}' <<< "${STANDARD_LIST}")

basic_list=$(awk'{printf”%s/%s/%s/%s\n“,$1,$2,$3,$4}'如果需要设置
bash
变量,请不要使用
awk
。直接在
bash
中工作,使用类似

declare -a tmp tmp2 tmp3 tmp4
while read -a f; do
    printf -v str '%s/%s/%s/%s' "${f[@]:0:4}"
    tmp+=$str
    tmp2+=${str%/*}
    tmp3+=${str%/*/*}
    tmp4+=${str%/*/*/*}
done <<< "$STANDARD_LIST"
basic_list=$(printf '%s\n' "${tmp[@]}")
basic_list2=$(printf '%s\n' "${tmp2[@]}")
basic_list3=$(printf '%s\n' "${tmp3[@]}")
basic_list4=$(printf '%s\n' "${tmp4[@]}")
declare-a tmp tmp2 tmp3 tmp4
当读的时候-一个f;做
printf-v str'%s/%s/%s/%s'${f[@]:0:4}
tmp+=$str
tmp2+=${str%/*}
tmp3+=${str%/*/*}
tmp4+=${str%/*/*/*}

完成如果您需要设置
bash
变量,请不要使用
awk
。直接在
bash
中使用

declare -a tmp tmp2 tmp3 tmp4
while read -a f; do
    printf -v str '%s/%s/%s/%s' "${f[@]:0:4}"
    tmp+=$str
    tmp2+=${str%/*}
    tmp3+=${str%/*/*}
    tmp4+=${str%/*/*/*}
done <<< "$STANDARD_LIST"
basic_list=$(printf '%s\n' "${tmp[@]}")
basic_list2=$(printf '%s\n' "${tmp2[@]}")
basic_list3=$(printf '%s\n' "${tmp3[@]}")
basic_list4=$(printf '%s\n' "${tmp4[@]}")
declare-a tmp tmp2 tmp3 tmp4
当读的时候-一个f;做
printf-v str'%s/%s/%s/%s'${f[@]:0:4}
tmp+=$str
tmp2+=${str%/*}
tmp3+=${str%/*/*}
tmp4+=${str%/*/*/*}

完成我不确定您想要实现什么,但是一旦有了bash数组,您就可以对它进行切片,例如

$ source=( a b c d e f )
$ echo "${source[@]}"
a b c d e f

$ echo "${source[@]:0:4}"
a b c d

$ echo "${source[@]:0:3}"
a b c

$ echo "${source[@]:0:2}"
a b

$ echo "${source[@]:0:1}"
a

您已经定义了四个变量,也许您根本不需要它们。

我不确定您想要实现什么,但是一旦有了bash数组,您就可以对其进行切片,例如

$ source=( a b c d e f )
$ echo "${source[@]}"
a b c d e f

$ echo "${source[@]:0:4}"
a b c d

$ echo "${source[@]:0:3}"
a b c

$ echo "${source[@]:0:2}"
a b

$ echo "${source[@]:0:1}"
a

你已经定义了四个变量,也许你根本不需要它们。

啊,好吧,我没意识到我能做到,那太好了谢谢,好吧,我没意识到我能做到,那太好了,谢谢你了,这可能行得通,但遗憾的是,$STANDARD_列表的格式不是那样的,谢谢你的帮助,这可能行得通,但遗憾的是$STANDARD_列表的格式不是这样的顺便说一句,谢谢你的帮助