echo 相同的字符串 85 次,其中包含一些升序数字

echo 相同的字符串 85 次,其中包含一些升序数字

我必须创建一个包含 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

答案1

您只想生成 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')

答案2

我不确定ts对你有什么作用,但如果你可以使用 bash 数组而不是按名称编号的变量,这应该可以满足你的要求:

#!/bin/bash

for n in {1..85}
do
    x=$(</home/user_files/out${n}.txt)
    y=$(</home/dept_files/dep${n}.txt)
    user_dep[$n]=$(echo " model: \$model_${n}  user: $x department: $y License_Used: \$p_out${n}" | ts '%Y-%m-%d %H:%M:%S')
done

# and show results
for n in {1..85}
do
    echo user_dep${n} = ${user_dep[$n]}
done

相关内容