The $@ variable is not working in a for loop, trying to iterate through a users list
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
PROBLEM:
I can't get the $@
variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt
each on its own line.
Here is the file:
something78
something79
something7
dagny
oli
bjarni
toti
stefan_hlynur
jessie
Here is the test code:
#!/bin/bash
prepare_USERS()
prepare_USERS "$@"
#for user in "$@"
#do
# echo "$user" >> USERS.txt
#done
for user in USERS.txt
do
printf "%s" $user
done
Here are the arguments I pass:
./somethingDELETEme.sh jessie henry allison jason
CURRENT output:
$./somethingDELETEme.sh jessie henry allison jason
jessie henry allison jason
jessie
EXPECTED output:
The loop loops through all names from the argument list and writes it to the file USERS.txt
.
QUESTION:
I have used this variable ($@
) before and never had this problem.
Why is the loop not iterating through all names in the argument list ($@
) and how is the right way coding this?
HERE IS THE REAL CODE:
prepare_USERS()
echo "writing to USERS.txt failed"; exit 127
done
bash shell-script variable for programming
add a comment |Â
up vote
0
down vote
favorite
PROBLEM:
I can't get the $@
variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt
each on its own line.
Here is the file:
something78
something79
something7
dagny
oli
bjarni
toti
stefan_hlynur
jessie
Here is the test code:
#!/bin/bash
prepare_USERS()
prepare_USERS "$@"
#for user in "$@"
#do
# echo "$user" >> USERS.txt
#done
for user in USERS.txt
do
printf "%s" $user
done
Here are the arguments I pass:
./somethingDELETEme.sh jessie henry allison jason
CURRENT output:
$./somethingDELETEme.sh jessie henry allison jason
jessie henry allison jason
jessie
EXPECTED output:
The loop loops through all names from the argument list and writes it to the file USERS.txt
.
QUESTION:
I have used this variable ($@
) before and never had this problem.
Why is the loop not iterating through all names in the argument list ($@
) and how is the right way coding this?
HERE IS THE REAL CODE:
prepare_USERS()
echo "writing to USERS.txt failed"; exit 127
done
bash shell-script variable for programming
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
PROBLEM:
I can't get the $@
variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt
each on its own line.
Here is the file:
something78
something79
something7
dagny
oli
bjarni
toti
stefan_hlynur
jessie
Here is the test code:
#!/bin/bash
prepare_USERS()
prepare_USERS "$@"
#for user in "$@"
#do
# echo "$user" >> USERS.txt
#done
for user in USERS.txt
do
printf "%s" $user
done
Here are the arguments I pass:
./somethingDELETEme.sh jessie henry allison jason
CURRENT output:
$./somethingDELETEme.sh jessie henry allison jason
jessie henry allison jason
jessie
EXPECTED output:
The loop loops through all names from the argument list and writes it to the file USERS.txt
.
QUESTION:
I have used this variable ($@
) before and never had this problem.
Why is the loop not iterating through all names in the argument list ($@
) and how is the right way coding this?
HERE IS THE REAL CODE:
prepare_USERS()
echo "writing to USERS.txt failed"; exit 127
done
bash shell-script variable for programming
PROBLEM:
I can't get the $@
variable to do what I want in a for loop, the loop only finds one name in the file while looping, it should loop through all the arguments and write them to the file USERS.txt
each on its own line.
Here is the file:
something78
something79
something7
dagny
oli
bjarni
toti
stefan_hlynur
jessie
Here is the test code:
#!/bin/bash
prepare_USERS()
prepare_USERS "$@"
#for user in "$@"
#do
# echo "$user" >> USERS.txt
#done
for user in USERS.txt
do
printf "%s" $user
done
Here are the arguments I pass:
./somethingDELETEme.sh jessie henry allison jason
CURRENT output:
$./somethingDELETEme.sh jessie henry allison jason
jessie henry allison jason
jessie
EXPECTED output:
The loop loops through all names from the argument list and writes it to the file USERS.txt
.
QUESTION:
I have used this variable ($@
) before and never had this problem.
Why is the loop not iterating through all names in the argument list ($@
) and how is the right way coding this?
HERE IS THE REAL CODE:
prepare_USERS()
echo "writing to USERS.txt failed"; exit 127
done
bash shell-script variable for programming
bash shell-script variable for programming
edited 3 mins ago
muru
34.1k578147
34.1k578147
asked 10 mins ago
somethingSomething
1,59993055
1,59993055
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
The problem is with the incorrect usage of exit 127
in your for loop, which is exiting after the first for-loop iteration. You need to group the echo
statement and the exit
as a compound block under ..
to prevent this.
echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127;
Without this grouping what happens is the ||
defined applies only for the echo
command and always run the exit
command no matter if write to fail passed or failed.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
The problem is with the incorrect usage of exit 127
in your for loop, which is exiting after the first for-loop iteration. You need to group the echo
statement and the exit
as a compound block under ..
to prevent this.
echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127;
Without this grouping what happens is the ||
defined applies only for the echo
command and always run the exit
command no matter if write to fail passed or failed.
add a comment |Â
up vote
2
down vote
The problem is with the incorrect usage of exit 127
in your for loop, which is exiting after the first for-loop iteration. You need to group the echo
statement and the exit
as a compound block under ..
to prevent this.
echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127;
Without this grouping what happens is the ||
defined applies only for the echo
command and always run the exit
command no matter if write to fail passed or failed.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
The problem is with the incorrect usage of exit 127
in your for loop, which is exiting after the first for-loop iteration. You need to group the echo
statement and the exit
as a compound block under ..
to prevent this.
echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127;
Without this grouping what happens is the ||
defined applies only for the echo
command and always run the exit
command no matter if write to fail passed or failed.
The problem is with the incorrect usage of exit 127
in your for loop, which is exiting after the first for-loop iteration. You need to group the echo
statement and the exit
as a compound block under ..
to prevent this.
echo "$user" >> USERS.txt || echo "writing to USERS.txt failed"; exit 127;
Without this grouping what happens is the ||
defined applies only for the echo
command and always run the exit
command no matter if write to fail passed or failed.
answered 2 mins ago
Inian
2,998822
2,998822
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476193%2fthe-variable-is-not-working-in-a-for-loop-trying-to-iterate-through-a-users%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password