很难在一行回声中提及多个通话

时间:2019-05-01 01:59:07

标签: bash sh

我的问题是回声无法回声,例如:“ $ prefix_ $ suffix”。如果这改变了事情,这是在学校上课的作业。

我已经尝试过例如“ $ prefix _ $ suffix”,但是会在前缀和后缀之间创建一个空格

#!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”被视为一个呼叫,而不是两个和“ $前缀_ $ suffix”在前缀和后缀之间添加一个空格,这是不可接受的。

即使您看上去内向,因此我添加了注释使其易于理解,非常感谢您的帮助。

如果您不了解并想提供帮助,请随时提问!

1 个答案:

答案 0 :(得分:1)

这将满足您的要求:

echo "${prefix}_${suffix}"
相关问题