我遇到的问题是回显不能回显,例如:"$prefix_$后缀"。这是学校班级的作业,如果这改变了事情。
我已经尝试过例如"$prefix_$后缀",但这会在前缀和后缀之间创建一个空格
#!bin/bash
read -p "Username prefix: " prefix
read -p "Amount of users: " amount
read -p "Name of file to store, include extension (e.g test.txt): " filename
touch "$filename"
new="$amount"
suffix=0
state=true
while [ state=true ] ; do
#in this function i reverse the user input amount of users so it appears as user 1,2,3 (and so on) in the first line of the text file that is also user input.
if [ "$new" -ge 1 ] ; then
newpass="$(gpg --gen-random --armor 1 12)"
#reversing process, making the suffix start at 1 so user 1 gets assigned suffix 1 for the username and i decrease the "new" variable that gets set to "$amount" so the while loop isn't indefinite
new=`expr "$new" - 1`
suffix=`expr "$suffix" + 1`
echo -n "$prefix" >> "$filename"
echo -n "_$suffix" >> "$filename"
echo -n " " >> "$filename"
echo "$newpass" >> "$filename"
echo -e >> "$filename"
elif [ "$new" -eq 0 ] ; then
break
fi
done
运行此 bash 会产生 5 行,例如:
re_1 UlrZW3jB5L9zt6Nf
依此类推,具体取决于您在输入中选择的用户数量
但是,下一个任务是使用用户名创建用户,在本例中re_1使用密码:UlrZW3jB5L9zt6Nf。这就是我所做的笨拙的回声工作不起作用的地方。我尝试做 useradd -u "$prefix_$suffix" 和 "$prefix $suffix",这些都不起作用,因为"$prefix$suffix"被视为一个调用而不是两个调用,并且"$prefix _$suffix"在前缀和后缀之间添加一个空格是不可接受的。
即使这对您来说看起来很内向,因此我添加了评论以使其易于理解,非常感谢帮助。
如果您不理解并想提供帮助,请随时提出问题!
这将执行您想要的操作:
echo "${prefix}_${suffix}"