6

I'm pretty new to bash scripting and I'm attempting to write a script that does some basic operations.

I want to check certain conditions and if they are met, terminate the script. So for example, I want to check whether the zip of files was successful:

echo "Zipping file..."
for file in $fileList;
    do
        echo $file | zip -v $archive -@
        if [[ $? != 0 ]];
            then
                echo "Error creating zip"
                exit 1
        fi
    done

What happens though is that the exit 1 signal causes the ssh connection to terminate as well:

Zipping file...

Command 'zip' not found, but can be installed with:

sudo apt install zip

Error creating zip
Connection to 3.137.7.52 closed.

What's the correct way to terminate a script without also disconnecting from the server?

3
  • 2
    just break or return out of the loop instead Commented Jan 14, 2020 at 9:48
  • 1
    I would close this as a duplicate of this or this, but that's on another site in the StackExchange Commented Jan 14, 2020 at 9:48
  • 1
    I'm voting to close this question as off-topic because it has been answered on several other SE sites. Commented Jan 14, 2020 at 9:52

1 Answer 1

3

If you wrap it all in a script with shebang #!/bin/bash than exit 1 will be fine but if you run this as a oneliner directly in console then this exit 1 means exit from console, and that would break ssh connection obvy

cat > ziper.sh << \EOF
#!/bin/bash
echo "Zipping file..."
for file in $fileList;
    do
        echo $file | zip -v $archive -@
        if [[ $? != 0 ]];
            then
                echo "Error creating zip"
                exit 1
        fi
    done
EOF

./ziper.sh

In oneliner use break

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

Comments

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.