An option is not seeing an argument (that is a variable) in bash, please help? [duplicate]
This question already has an answer here:
How do I assign the output of a command to a variable?
1 answer
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
        psswd= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2 
        full_name= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3 
        group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4 
        second_group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5 
        sudo useradd $username -m -g $group -s /bin/bash -c $full_name
        if [ second_group = LPGestionnaires ]
        then 
                usermod -a -G $second_group $user
        fi
        #echo "$username:$psswd" | chpasswd
        ((counter++))
done
echo Execution complete
The part where it says sudo useradd $username -m -g $group -s /bin/bash -c $full_name is the part that isn't working, my -g option isn't seeing the variable $group argument as an argument, when I execute my script it returns this: useradd: group '-s' does not exist
I'm pulling the data from a .csv file that is located correctly.
If anyone could help that would be great!
Thanks!
command-line bash scripts
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
                    marked as duplicate by muru
    StackExchange.ready(function() {
        if (StackExchange.options.isMobile) return;
        $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
            var $hover = $(this).addClass('hover-bound'),
                $msg = $hover.siblings('.dupe-hammer-message');
            
            $hover.hover(
                function() {
                    $hover.showInfoMessage('', {
                        messageElement: $msg.clone().show(),
                        transient: false,
                        position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
                        dismissable: false,
                        relativeToBody: true
                    });
                },
                function() {
                    StackExchange.helpers.removeMessages();
                }
            );
        });
    });
 Dec 17 at 1:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I assign the output of a command to a variable?
1 answer
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
        psswd= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2 
        full_name= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3 
        group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4 
        second_group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5 
        sudo useradd $username -m -g $group -s /bin/bash -c $full_name
        if [ second_group = LPGestionnaires ]
        then 
                usermod -a -G $second_group $user
        fi
        #echo "$username:$psswd" | chpasswd
        ((counter++))
done
echo Execution complete
The part where it says sudo useradd $username -m -g $group -s /bin/bash -c $full_name is the part that isn't working, my -g option isn't seeing the variable $group argument as an argument, when I execute my script it returns this: useradd: group '-s' does not exist
I'm pulling the data from a .csv file that is located correctly.
If anyone could help that would be great!
Thanks!
command-line bash scripts
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
                    marked as duplicate by muru
    StackExchange.ready(function() {
        if (StackExchange.options.isMobile) return;
        $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
            var $hover = $(this).addClass('hover-bound'),
                $msg = $hover.siblings('.dupe-hammer-message');
            
            $hover.hover(
                function() {
                    $hover.showInfoMessage('', {
                        messageElement: $msg.clone().show(),
                        transient: false,
                        position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
                        dismissable: false,
                        relativeToBody: true
                    });
                },
                function() {
                    StackExchange.helpers.removeMessages();
                }
            );
        });
    });
 Dec 17 at 1:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I assign the output of a command to a variable?
1 answer
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
        psswd= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2 
        full_name= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3 
        group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4 
        second_group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5 
        sudo useradd $username -m -g $group -s /bin/bash -c $full_name
        if [ second_group = LPGestionnaires ]
        then 
                usermod -a -G $second_group $user
        fi
        #echo "$username:$psswd" | chpasswd
        ((counter++))
done
echo Execution complete
The part where it says sudo useradd $username -m -g $group -s /bin/bash -c $full_name is the part that isn't working, my -g option isn't seeing the variable $group argument as an argument, when I execute my script it returns this: useradd: group '-s' does not exist
I'm pulling the data from a .csv file that is located correctly.
If anyone could help that would be great!
Thanks!
command-line bash scripts
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This question already has an answer here:
How do I assign the output of a command to a variable?
1 answer
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
        psswd= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2 
        full_name= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3 
        group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4 
        second_group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5 
        sudo useradd $username -m -g $group -s /bin/bash -c $full_name
        if [ second_group = LPGestionnaires ]
        then 
                usermod -a -G $second_group $user
        fi
        #echo "$username:$psswd" | chpasswd
        ((counter++))
done
echo Execution complete
The part where it says sudo useradd $username -m -g $group -s /bin/bash -c $full_name is the part that isn't working, my -g option isn't seeing the variable $group argument as an argument, when I execute my script it returns this: useradd: group '-s' does not exist
I'm pulling the data from a .csv file that is located correctly.
If anyone could help that would be great!
Thanks!
This question already has an answer here:
How do I assign the output of a command to a variable?
1 answer
command-line bash scripts
command-line bash scripts
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Dec 17 at 0:49
wjandrea
8,24342258
8,24342258
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Dec 16 at 23:41
moltenmath
134
134
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
                    marked as duplicate by muru
    StackExchange.ready(function() {
        if (StackExchange.options.isMobile) return;
        $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
            var $hover = $(this).addClass('hover-bound'),
                $msg = $hover.siblings('.dupe-hammer-message');
            
            $hover.hover(
                function() {
                    $hover.showInfoMessage('', {
                        messageElement: $msg.clone().show(),
                        transient: false,
                        position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
                        dismissable: false,
                        relativeToBody: true
                    });
                },
                function() {
                    StackExchange.helpers.removeMessages();
                }
            );
        });
    });
 Dec 17 at 1:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
                    marked as duplicate by muru
    StackExchange.ready(function() {
        if (StackExchange.options.isMobile) return;
        $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
            var $hover = $(this).addClass('hover-bound'),
                $msg = $hover.siblings('.dupe-hammer-message');
            
            $hover.hover(
                function() {
                    $hover.showInfoMessage('', {
                        messageElement: $msg.clone().show(),
                        transient: false,
                        position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
                        dismissable: false,
                        relativeToBody: true
                    });
                },
                function() {
                    StackExchange.helpers.removeMessages();
                }
            );
        });
    });
 Dec 17 at 1:09
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
It seems you wanted to assign the result of the head ... command to the variable username here:
username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1
That is incorrect syntax. Correct it like this:
username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
And then do the same for the other variables too, which all have the same problem.
Also, change the sudo useradd command like this:
sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
Variables used in command line arguments should usually be double-quoted to avoid word splitting.
add a comment |
The final code is this for anyone that is curious:
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
        psswd=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2)
        full_name=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3)
        group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4)
        second_group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5)
        sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
        if [ "$second_group" = LPGestionnaires ]
        then 
                sudo usermod -a -G LPGestionnaires "$username"
        fi
        echo "$username:$psswd" | sudo chpasswd
        ((counter++))
done
echo Execution complete
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
You're almost re-inventing thenewuserscommand here - at the very least, your script could be made somewhat neater by using awhileloop (something likewhile IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)
– steeldriver
Dec 17 at 1:03
3
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:done < <(sed -n '2,19 p' ./user_sheet.csv)
– wjandrea
Dec 17 at 1:06
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
add a comment |
                                2 Answers
                                2
                        
active
oldest
votes
                                2 Answers
                                2
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
It seems you wanted to assign the result of the head ... command to the variable username here:
username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1
That is incorrect syntax. Correct it like this:
username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
And then do the same for the other variables too, which all have the same problem.
Also, change the sudo useradd command like this:
sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
Variables used in command line arguments should usually be double-quoted to avoid word splitting.
add a comment |
It seems you wanted to assign the result of the head ... command to the variable username here:
username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1
That is incorrect syntax. Correct it like this:
username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
And then do the same for the other variables too, which all have the same problem.
Also, change the sudo useradd command like this:
sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
Variables used in command line arguments should usually be double-quoted to avoid word splitting.
add a comment |
It seems you wanted to assign the result of the head ... command to the variable username here:
username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1
That is incorrect syntax. Correct it like this:
username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
And then do the same for the other variables too, which all have the same problem.
Also, change the sudo useradd command like this:
sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
Variables used in command line arguments should usually be double-quoted to avoid word splitting.
It seems you wanted to assign the result of the head ... command to the variable username here:
username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1
That is incorrect syntax. Correct it like this:
username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
And then do the same for the other variables too, which all have the same problem.
Also, change the sudo useradd command like this:
sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
Variables used in command line arguments should usually be double-quoted to avoid word splitting.
edited Dec 16 at 23:52
answered Dec 16 at 23:44
janos
3,7261445
3,7261445
add a comment |
add a comment |
The final code is this for anyone that is curious:
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
        psswd=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2)
        full_name=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3)
        group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4)
        second_group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5)
        sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
        if [ "$second_group" = LPGestionnaires ]
        then 
                sudo usermod -a -G LPGestionnaires "$username"
        fi
        echo "$username:$psswd" | sudo chpasswd
        ((counter++))
done
echo Execution complete
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
You're almost re-inventing thenewuserscommand here - at the very least, your script could be made somewhat neater by using awhileloop (something likewhile IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)
– steeldriver
Dec 17 at 1:03
3
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:done < <(sed -n '2,19 p' ./user_sheet.csv)
– wjandrea
Dec 17 at 1:06
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
add a comment |
The final code is this for anyone that is curious:
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
        psswd=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2)
        full_name=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3)
        group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4)
        second_group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5)
        sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
        if [ "$second_group" = LPGestionnaires ]
        then 
                sudo usermod -a -G LPGestionnaires "$username"
        fi
        echo "$username:$psswd" | sudo chpasswd
        ((counter++))
done
echo Execution complete
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
You're almost re-inventing thenewuserscommand here - at the very least, your script could be made somewhat neater by using awhileloop (something likewhile IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)
– steeldriver
Dec 17 at 1:03
3
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:done < <(sed -n '2,19 p' ./user_sheet.csv)
– wjandrea
Dec 17 at 1:06
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
add a comment |
The final code is this for anyone that is curious:
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
        psswd=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2)
        full_name=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3)
        group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4)
        second_group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5)
        sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
        if [ "$second_group" = LPGestionnaires ]
        then 
                sudo usermod -a -G LPGestionnaires "$username"
        fi
        echo "$username:$psswd" | sudo chpasswd
        ((counter++))
done
echo Execution complete
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
The final code is this for anyone that is curious:
#!/bin/bash
counter=2
while [ $counter -lt 19 ]
do
        username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
        psswd=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2)
        full_name=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3)
        group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4)
        second_group=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5)
        sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
        if [ "$second_group" = LPGestionnaires ]
        then 
                sudo usermod -a -G LPGestionnaires "$username"
        fi
        echo "$username:$psswd" | sudo chpasswd
        ((counter++))
done
echo Execution complete
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Dec 17 at 0:35
moltenmath
134
134
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
moltenmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
You're almost re-inventing thenewuserscommand here - at the very least, your script could be made somewhat neater by using awhileloop (something likewhile IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)
– steeldriver
Dec 17 at 1:03
3
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:done < <(sed -n '2,19 p' ./user_sheet.csv)
– wjandrea
Dec 17 at 1:06
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
add a comment |
3
You're almost re-inventing thenewuserscommand here - at the very least, your script could be made somewhat neater by using awhileloop (something likewhile IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)
– steeldriver
Dec 17 at 1:03
3
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:done < <(sed -n '2,19 p' ./user_sheet.csv)
– wjandrea
Dec 17 at 1:06
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
3
3
You're almost re-inventing the
newusers command here - at the very least, your script could be made somewhat neater by using a while loop (something like while IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)– steeldriver
Dec 17 at 1:03
You're almost re-inventing the
newusers command here - at the very least, your script could be made somewhat neater by using a while loop (something like while IFS=';' read -r username passwd full_name group second_group; do stuff; done < ./user_sheet.csv)– steeldriver
Dec 17 at 1:03
3
3
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:
done < <(sed -n '2,19 p' ./user_sheet.csv)– wjandrea
Dec 17 at 1:06
@steeldriver I was just going to say something similar. For getting lines 2-19, I would use a sed command:
done < <(sed -n '2,19 p' ./user_sheet.csv)– wjandrea
Dec 17 at 1:06
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
@steeldriver my teacher wanted us to write the entire thing so I couldn't use a premade 'newuser' however you are right that it could have been a lot cleaner and that I'm repeating a lot of things
– moltenmath
Dec 17 at 23:53
add a comment |