How to use the “for .. in” loop
#!/bin/bash
if [ ! $# -eq 2 ];
then echo "You have not inputted the correct amound of arguments.
usage: $0 file user
Where file is the file to search
and user is the user to find"
fi
if [ ! -e $1 ];
then echo "You have inputted an invalid filename.
usage: $ file user
Where file is the file to search
and user is the user to find"
fi
let count=0
for line in `cat $1`; do
count=`expr $count + 1`
if [ "$line" == "$2" ]; then
echo "$2 found on line: $count"
exit 0
else
echo "Would you like to insert this username? y/n"
read answer
answer=`echo $answer | tr [a-z] [A-Z]`
if [ "$answer" != "y" ]; then
cat "$answer" >> "/classlist.txt"
else
echo "That's fine. Program ending ..."
exit 0
fi
fi
done
my "for in" loop doesn't do what it should do. the loop should take data from the text file (btw the text file is just a list of names broken up by line breaks) and assign it to the variable "line." when the "for in" loop ends, it should reassign the "line" variable to the next line in the text file. However, this is not the case. the script only ever reads the first data entry in the text file. did I use the loop incorrectly?
linux bash shell-script
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
#!/bin/bash
if [ ! $# -eq 2 ];
then echo "You have not inputted the correct amound of arguments.
usage: $0 file user
Where file is the file to search
and user is the user to find"
fi
if [ ! -e $1 ];
then echo "You have inputted an invalid filename.
usage: $ file user
Where file is the file to search
and user is the user to find"
fi
let count=0
for line in `cat $1`; do
count=`expr $count + 1`
if [ "$line" == "$2" ]; then
echo "$2 found on line: $count"
exit 0
else
echo "Would you like to insert this username? y/n"
read answer
answer=`echo $answer | tr [a-z] [A-Z]`
if [ "$answer" != "y" ]; then
cat "$answer" >> "/classlist.txt"
else
echo "That's fine. Program ending ..."
exit 0
fi
fi
done
my "for in" loop doesn't do what it should do. the loop should take data from the text file (btw the text file is just a list of names broken up by line breaks) and assign it to the variable "line." when the "for in" loop ends, it should reassign the "line" variable to the next line in the text file. However, this is not the case. the script only ever reads the first data entry in the text file. did I use the loop incorrectly?
linux bash shell-script
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Your script runs a loop over the "lines" in the file, and on each line, either exits (if$2was found), or asks to insert the username. If you mean to look through the whole file before asking to add something, you'll have to move the adding part out of the loop. You might also want to fix the indentation of the code to better see how the different parts of logic are nested. That, and run the script through [shellcheck.net], it has a number of obsolete, nonstandard or otherwise bad constructs (includingfor line in $(cat $1)itself)
– ilkkachu
2 hours ago
add a comment |
#!/bin/bash
if [ ! $# -eq 2 ];
then echo "You have not inputted the correct amound of arguments.
usage: $0 file user
Where file is the file to search
and user is the user to find"
fi
if [ ! -e $1 ];
then echo "You have inputted an invalid filename.
usage: $ file user
Where file is the file to search
and user is the user to find"
fi
let count=0
for line in `cat $1`; do
count=`expr $count + 1`
if [ "$line" == "$2" ]; then
echo "$2 found on line: $count"
exit 0
else
echo "Would you like to insert this username? y/n"
read answer
answer=`echo $answer | tr [a-z] [A-Z]`
if [ "$answer" != "y" ]; then
cat "$answer" >> "/classlist.txt"
else
echo "That's fine. Program ending ..."
exit 0
fi
fi
done
my "for in" loop doesn't do what it should do. the loop should take data from the text file (btw the text file is just a list of names broken up by line breaks) and assign it to the variable "line." when the "for in" loop ends, it should reassign the "line" variable to the next line in the text file. However, this is not the case. the script only ever reads the first data entry in the text file. did I use the loop incorrectly?
linux bash shell-script
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
#!/bin/bash
if [ ! $# -eq 2 ];
then echo "You have not inputted the correct amound of arguments.
usage: $0 file user
Where file is the file to search
and user is the user to find"
fi
if [ ! -e $1 ];
then echo "You have inputted an invalid filename.
usage: $ file user
Where file is the file to search
and user is the user to find"
fi
let count=0
for line in `cat $1`; do
count=`expr $count + 1`
if [ "$line" == "$2" ]; then
echo "$2 found on line: $count"
exit 0
else
echo "Would you like to insert this username? y/n"
read answer
answer=`echo $answer | tr [a-z] [A-Z]`
if [ "$answer" != "y" ]; then
cat "$answer" >> "/classlist.txt"
else
echo "That's fine. Program ending ..."
exit 0
fi
fi
done
my "for in" loop doesn't do what it should do. the loop should take data from the text file (btw the text file is just a list of names broken up by line breaks) and assign it to the variable "line." when the "for in" loop ends, it should reassign the "line" variable to the next line in the text file. However, this is not the case. the script only ever reads the first data entry in the text file. did I use the loop incorrectly?
linux bash shell-script
linux bash shell-script
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 hours ago
Rui F Ribeiro
41.4k1481140
41.4k1481140
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
JoshJosh
1
1
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Your script runs a loop over the "lines" in the file, and on each line, either exits (if$2was found), or asks to insert the username. If you mean to look through the whole file before asking to add something, you'll have to move the adding part out of the loop. You might also want to fix the indentation of the code to better see how the different parts of logic are nested. That, and run the script through [shellcheck.net], it has a number of obsolete, nonstandard or otherwise bad constructs (includingfor line in $(cat $1)itself)
– ilkkachu
2 hours ago
add a comment |
Your script runs a loop over the "lines" in the file, and on each line, either exits (if$2was found), or asks to insert the username. If you mean to look through the whole file before asking to add something, you'll have to move the adding part out of the loop. You might also want to fix the indentation of the code to better see how the different parts of logic are nested. That, and run the script through [shellcheck.net], it has a number of obsolete, nonstandard or otherwise bad constructs (includingfor line in $(cat $1)itself)
– ilkkachu
2 hours ago
Your script runs a loop over the "lines" in the file, and on each line, either exits (if
$2 was found), or asks to insert the username. If you mean to look through the whole file before asking to add something, you'll have to move the adding part out of the loop. You might also want to fix the indentation of the code to better see how the different parts of logic are nested. That, and run the script through [shellcheck.net], it has a number of obsolete, nonstandard or otherwise bad constructs (including for line in $(cat $1) itself)– ilkkachu
2 hours ago
Your script runs a loop over the "lines" in the file, and on each line, either exits (if
$2 was found), or asks to insert the username. If you mean to look through the whole file before asking to add something, you'll have to move the adding part out of the loop. You might also want to fix the indentation of the code to better see how the different parts of logic are nested. That, and run the script through [shellcheck.net], it has a number of obsolete, nonstandard or otherwise bad constructs (including for line in $(cat $1) itself)– ilkkachu
2 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Josh is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505958%2fhow-to-use-the-for-in-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Josh is a new contributor. Be nice, and check out our Code of Conduct.
Josh is a new contributor. Be nice, and check out our Code of Conduct.
Josh is a new contributor. Be nice, and check out our Code of Conduct.
Josh is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505958%2fhow-to-use-the-for-in-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your script runs a loop over the "lines" in the file, and on each line, either exits (if
$2was found), or asks to insert the username. If you mean to look through the whole file before asking to add something, you'll have to move the adding part out of the loop. You might also want to fix the indentation of the code to better see how the different parts of logic are nested. That, and run the script through [shellcheck.net], it has a number of obsolete, nonstandard or otherwise bad constructs (includingfor line in $(cat $1)itself)– ilkkachu
2 hours ago