#!/bin/bash
if [ -f "$1" ]
then
    for users in 'cat $1'
    do
        useradd $users
    done
else
    echo "input is not a file"
fi
            Asked
            
        
        
            Active
            
        
            Viewed 167 times
        
    -1
            
            
        - 
                    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 Answers
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
- 
                    Hi Jonas Eberle, Thanks for sharing this answer. This worked. Thank you for helping. – parth5194 Jan 31 '20 at 12:50
 
    