0

My shell script, It executes its inner loop correctly, but only execute its outer loop once.

Please kindly check my script, why it does not work.

OS: Amazon EC2 Linux

I created shell script, to delete files, from a directory, with filenames, that are not exist in my created TXT file.

for entry in "/home/ec2-user/upload/upload/*"
do
    exist=false

    file="/home/ec2-user/upload/requiredjpg.txt"
    while IFS= read -r line
    do
        if [ "$line" = "$entry" ]
        then
            echo "same"
        else
            echo "not same"
        fi
    done <"$file"

    echo $exist
    if [ $exist = false ]
    then
        echo $exist
    fi
done
0

2 Answers 2

1

Expression "/home/ec2-user/upload/upload/*" is interpreted, literally, as string /home/ec2-user/upload/upload/*. Obviously, you expected it to be expanded to an array of paths matching the wildcard expression. But, in double quotes, the asterisk character has different meaning, -- it is only used in shell parameter expansions (which is not present in the expression in question.)

What you really need is filename expansion. In order to activate it, you simply need to put the asterisk character outside the double quotes:

"/home/ec2-user/upload/upload/"*

Note that you don't need the double quotes in this particular string, since there is no any special character in it that needs to be escaped (quoted).

Sign up to request clarification or add additional context in comments.

4 Comments

but this part: if [ "$line" = "$entry" ] it always consider both variable as not equal, even if it is equal, anyone know the reason?
@Hendra, I'm not sure I understand what you are trying to do, but the expression if [ "$line" = "$entry" ] compares strings and returns true if and only if the strings are equal. The code in the question iterates (supposed to) all files in /home/ec2-user/upload/upload/ directory, then compares the file paths with each line of text in /home/ec2-user/upload/requiredjpg.txt. It is just possible that you really wanted to compare "/home/ec2-user/upload/requiredjpg.txt" (as a string) with "$entry"; in that case, you don't need the while loop.
@Hendra, also, I would suggest printing more information, not just same and not same, but somethiing like printf '%s equals %s\n' "$line" "$entry". Alternatively, you can turn on debugging mode with set -x command.
@Hendra, as an aside, you check the value of $exist variable, but do not set it anywhere after initialization. Also, you might want to break the loop when "the file is found". And yes, if you are only searching for specific path/filename, then the find command would be much more convenient, e.g.: find /home/ec2-user/upload/upload/ -type f -name 'requiredjpg.txt'
0

There is misplaced 'do', in the inner loop. The 'if' statements should come under it.

while IFS= read -r line 
do
    if [ "$line" = "$entry" ]

1 Comment

Sorry about that. OP originally pasted an image which I transcribed to text. I missed the do. It is there in the original code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.