-1
#!/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?

  • 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 Mar 12 at 20:49
0

Short answer: Change your exit 0 to continue

To be sure your for loop is working correctly, you can do a short test:
count=0 for line in cat $1 do count=expr $count + 1 echo $line done echo "Counted $count lines"

If that gives you all the lines in your file, and the accurate count, then your for loop is working correctly. (seems correct. Not the acceptable standard, but I prefer this format myself)

The first issue is, this:
if [ "$line" == "$2" ]; then echo "$2 found on line: $count" exit 0 The exit 0 says exit the script (i.e. do nothing more.) You could just eliminate this line all together as it will drop out of the if and hit the done which will read the next line. Or you could change it to continue which will return to the top of the loop and read the next line.

The second issue is:
if [ "$answer" != "y" ]; then cat "$answer" >> "/classlist.txt" else echo "That's fine. Program ending ..." exit 0 fi If you really want to exit the script, your exit 0 is fine. If you want to break out of the loop, then this should be a break. If you want to go to the top of the loop to read the next line, it should be a continue.

To see how the different ways of exiting the loop affect your code, add the line echo "Finished loop" to the end of your script. Experiment with the break, continue, and exit 0 options to see which one prints "Finished Loop"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.