-1
#!/bin/bash
if [ -f "$1" ]
then
    for users in 'cat $1'
    do
        useradd $users
    done
else
    echo "input is not a file"
fi
kaylum
  • 13,833
  • 2
  • 22
  • 31
parth5194
  • 3
  • 2
  • Please post output as text and not as links or images - [reasoning](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – kaylum Jan 30 '20 at 09:52
  • Single quotes is not what you want. `'cat $1'` --> `$(cat $1)` or `\`cat $1\`` – kaylum Jan 30 '20 at 09:54
  • To substitute a command's output, that is `cat $1` here, we don't use single quotes; the correct syntax is `\`cat $1\`` or `$(cat $1)`, latter is more convenient – oguz ismail Jan 30 '20 at 09:55

1 Answers1

0

You just have to get the input for the do loop right:

#!/bin/bash
if [ -f "$1" ]
then
    for user in $(cat "$1")
    do
        useradd "$user"
    done
else
    echo "input is not a file"
fi

Remarks: this works for reading out a file word-by-word and I tried to keep your structure.

For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

Jonas Eberle
  • 2,835
  • 1
  • 15
  • 25