论坛里的一个兄弟要同时折腾两个变量,不知道如何折腾. 后来了解到可以用数组
都忘记shell还有数组了.
写了一个测试程序. 记录一下. 以后也有个印象.
#!/bin/bash declare -a tmp for i in $(find /bin -type f) do let c++ tmp[c]="$i" done len="${#tmp[@]}" #array length c=len for i in $(find /usr/bin -type f) do let c++ tmp[c]="$i" done i=0 while [ $i -le $len ] do let i+=1 echo "BIN:${tmp[i]}; USR:${tmp[i+len]}" done $ bash a.sh BIN:/bin/cp USR:/usr/bin/semodule_link BIN:/bin/redhat_lsb_init USR:/usr/bin/gst-launch BIN:/bin/readlink USR:/usr/bin/opgprof BIN:/bin/unicode_start USR:/usr/bin/eu-strings BIN:/bin/ps USR:/usr/bin/pango-querymodules-64 BIN:/bin/lsblk USR:/usr/bin/w BIN:/bin/sleep USR:/usr/bin/smbta-util BIN:/bin/setserial USR:/usr/bin/aserver BIN:/bin/unicode_stop USR:/usr/bin/patch BIN:/bin/cgget USR:/usr/bin/pdftops BIN:/bin/nano USR:/usr/bin/lzmainfo BIN:/bin/cgdelete USR:/usr/bin/gstack BIN:/bin/ln USR:/usr/bin/rngtest BIN:/bin/mkdir USR:/usr/bin/nfs4_setfacl BIN:/bin/mknod USR:/usr/bin/vimtutor BIN:/bin/dumpkeys USR:/usr/bin/op-check-perfevents BIN:/bin/ipcalc USR:/usr/bin/sedispol BIN:/bin/gzip USR:/usr/bin/reporter-mailx BIN:/bin/true USR:/usr/bin/tiff2pdf BIN:/bin/mv USR:/usr/bin/urlgrabber BIN:/bin/dbus-uuidgen USR:/usr/bin/groffer BIN:/bin/cgsnapshot USR:/usr/bin/smbcquotas BIN:/bin/env USR:/usr/bin/nfs4_getfacl BIN:/bin/arch USR:/usr/bin/repomanage BIN:/bin/echo USR:/usr/bin/eu-unstrip BIN:/bin/touch USR:/usr/bin/.ssh.hmac
脚本取第一次的结果长度为后面while的最大长度.
数组还可以直接
for i in ${array[@]}; do ... ; done
但要同时取两个变量就不好折腾了.
另外数组还有分片, 替换等高级功能, 这个目前没用到. 不写了.
还是折腾python为主...
附, 那边论坛版主的脚本. 使用三目运算等等, 更加简洁
#!/bin/bash bin=( $(find /bin -type f) ) ubin=( $(find /usr/bin -type f) ) (( len = ${#bin[@]} < ${#ubin[@]} ? ${#bin[@]} : ${#ubin[@]} )) for (( i = 0; i < len; i++ )); do echo "/bin: ${bin[$i]}" echo "/usr/bin: ${ubin[$i]}" done