LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   defensive Bash scripting (https://www.linuxquestions.org/questions/linux-newbie-8/defensive-bash-scripting-4175607344/)

Waris 06-05-2017 04:53 PM

defensive Bash scripting
 
Hi guys,

How do i start off my Bash script to check to see if the file im interested in(which is automatically generated by another service) is actually there and if not there how can i tell the it to stop the execution of the rest of the script and echo something like file or directory not found? in BASH.

Thanks in advance!

justmy2cents 06-05-2017 05:03 PM

Code:

-- test (use in a shell script to check to see if a given statement is true)
-- case ... in .. esac (use in a shell script to perform separate actions for a variety of cases)
-- break (use in a shell script to skip the rest of the commands in the loop and restart at the beggining of the loop
-- sleep 5s (pause for 5 seconds)
-- continue (use in a for, while, until, or select loop to stop the current iteration and start the next one


suicidaleggroll 06-05-2017 05:59 PM

Code:

if [[ ! -f filename ]]; then
  echo "file not found"
  exit 1
fi


Habitual 06-05-2017 06:20 PM

see "man test" also :)

BW-userx 06-05-2017 08:35 PM

Code:

if [[ -f filename ]]; then
echo "Got it yo!"
else
{
  echo "Not yet dude"
  exit 1
}
fi

that way you know either way. ;)

dave@burn-it.co.uk 06-06-2017 05:55 AM

That's done your homework for you!!

BW-userx 06-06-2017 07:36 AM

of course I (we, others) have to take into account you may not know anything about programming whatsoever.

Introduction

rtmistler 06-06-2017 07:45 AM

Quote:

Originally Posted by Waris (Post 5719398)
Hi guys,

How do i start off my Bash script to check to see if the file im interested in(which is automatically generated by another service) is actually there and if not there how can i tell the it to stop the execution of the rest of the script and echo something like file or directory not found? in BASH.

Thanks in advance!

Hi Waris and once again welcome to LQ.

You have done some bash scripting, as shown in your other question http://www.linuxquestions.org/questi...9/#post5718425, therefore I urge you to post your code here, or better describe what you have reviewed or tried.

As I mentioned in that other thread, here are a few bash reference links:
Bash Guide for Beginners
Advanced BASH Scripting Guide
My Bash Blog

Note that in Bash Beginners guide will tell you lots about file tests for things like making a determination as to whether a file exists and if it is a regular file or a directory. Please take the time to review these or other references for Bash.

I feel that if you persist with asking open ended questions, you'll possibly get confusing information back with members sometimes attempting to provide you wish some potentially useful code clips.

Habitual 06-06-2017 07:54 AM

Quote:

Originally Posted by BW-userx (Post 5719466)
Code:

if [[ -f filename ]]; then
echo "Got it yo!"
else
{
  echo "Not yet dude"
  exit 1
}
fi

that way you know either way. ;)

I saw that also. But I punted and guessed that suicidaleggroll only gave him "half" on purpose.
I did workup this exercise for shits and giggles.
Code:

touch LQRocks
if [[ ! -f LQRocks ]] ; then echo "Not found"; else echo Exists  ; fi
rm LQRocks
if [[ ! -f LQRocks ]] ; then echo "Not found"; else echo Exists  ; fi


BW-userx 06-06-2017 08:05 AM

I don't think I said too much because he still needs to figure out what (besides the shebang and whatnot) in order to get that to work in that if statement?


Quote:

Originally Posted by Habitual (Post 5719610)
I saw that also. But I punted and guessed that suicidaleggroll only gave him "half" on purpose.
I did workup this exercise for shits and giggles.
Code:

touch LQRocks
if [[ ! -f LQRocks ]] ; then echo "Not found"; else echo Exists  ; fi
rm LQRocks
if [[ ! -f LQRocks ]] ; then echo "Not found"; else echo Exists  ; fi


yours too can be changed to this.
Code:

[[ ! -f LQRocks ]] && echo "Not found"  || (echo Exists ; rm LQRocks)
or
[[ ! -f LQRocks ]] && echo "Not found" || echo "Exists"

which is not full proof programming in BASH because BASH does not have an ternary operator - but if the coder knows what he is working with he can get away with that. Depending on the circumstances.

Note:
why you got rm LQrocks when it returns that it is not there I do not know. of if it is then what takes place by the way you have it written?

@rtmistler

Oh I didn't realize it was that guy. I just looked at the link you gave to that page to reference his other post .... I don't find myself paying attention to the poster name, I look more at the problem presented.

Waris 06-06-2017 09:11 AM

Thank you guys for your responses. greatly appreciate it. This is a huge community and i am very happy for that. Also, rtMistler, I will post in the right forums. Sorry beginners mistake.

thanks again guys!!!

Waris 06-06-2017 09:35 AM

Quote:

Originally Posted by suicidaleggroll (Post 5719428)
Code:

if [[ ! -f filename ]]; then
  echo "file not found"
  exit 1
fi


This was exactly what i was looking for. Thank you. But what i cant understand is what is the ! and -f for? I am very new to the whole scripting world.:D

BW-userx 06-06-2017 09:42 AM

Quote:

Originally Posted by Waris (Post 5719658)
This was exactly what i was looking for. Thank you. But what i cant understand is what is the ! and -f for? I am very new to the whole scripting world.:D

Logical Not !
File test operators
Exit and Exit Status

Waris 06-06-2017 10:25 AM

Quote:

Originally Posted by suicidaleggroll (Post 5719428)
Code:

if [[ ! -f filename ]]; then
  echo "file not found"
  exit 1
fi


This was exactly what i was looking for. Thank you. But what i cant understand is what is the ! and -f for? I am very new to the whole scripting world.:D

Waris 06-06-2017 10:29 AM

Quote:

Originally Posted by BW-userx (Post 5719660)


Thanks! This is pretty straight forward! gracias compadres!


All times are GMT -5. The time now is 11:38 AM.