Bash

回顯相同的字元串 85 次,其中包含一些升序數字

  • August 24, 2016

我必須按以下格式創建一個包含 85 個條目的大文件:

user_dept1=$( while read -r x && read -r y <&3; do   echo " model: $model_1  user: $x department: $y License_Used: $p_out1"; done < /home/user_files/out1.txt 3</home/dept_files/dep1.txt | ts '%Y-%m-%d %H:%M:%S')

user_dept2=$( while read -r x && read -r y <&3; do   echo " model: $model_2  user: $x department: $y License_Used: $p_out2"; done < /home/user_files/out2.txt 3</home/dept_files/dep2.txt | ts '%Y-%m-%d %H:%M:%S')

依此類推,直到 user_dept85

user_dept85=$( while read -r x && read -r y <&3; do   echo " model: $model_85  user: $x department: $y License_Used: $p_out85"; done < /home/user_files/out85.txt 3</home/dept_files/dep85.txt | ts '%Y-%m-%d %H:%M:%S')

因此,下面的字元串每次都會從 1-85 重命名:

user_dep1 - user_dept85
$model_1 -  $model_85
$p_out1  - $p_out85
out1.txt - out85.txt
dep1.txt - dep85.txt

你只想生成 85 行程式碼?

for a in {1..85}
do
 echo "user_dept$a=\$( while read -r x && read -r y <&3; do   echo \" model: \$model_$a  user: \$x department: \$y License_Used: \$p_out$a\"; done < /home/user_files/out$a.txt 3</home/dept_files/dep$a.txt | ts '%Y-%m-%d %H:%M:%S')"
done > resulting_code

訣竅是確保觀察到引用。所以$and "" 字元需要被引用為\$and\"

結果輸出的前 3 行:

user_dept1=$( while read -r x && read -r y <&3; do   echo " model: $model_1  user: $x department: $y License_Used: $p_out1"; done < /home/user_files/out1.txt 3</home/dept_files/dep1.txt | ts '%Y-%m-%d %H:%M:%S')
user_dept2=$( while read -r x && read -r y <&3; do   echo " model: $model_2  user: $x department: $y License_Used: $p_out2"; done < /home/user_files/out2.txt 3</home/dept_files/dep2.txt | ts '%Y-%m-%d %H:%M:%S')
user_dept3=$( while read -r x && read -r y <&3; do   echo " model: $model_3  user: $x department: $y License_Used: $p_out3"; done < /home/user_files/out3.txt 3</home/dept_files/dep3.txt | ts '%Y-%m-%d %H:%M:%S')

引用自:https://unix.stackexchange.com/questions/305513