LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2017, 05:53 PM   #1
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Rep: Reputation: Disabled
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!
 
Old 06-05-2017, 06:03 PM   #2
justmy2cents
Member
 
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Blog Entries: 2

Rep: Reputation: Disabled
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

Last edited by justmy2cents; 06-06-2017 at 10:36 AM.
 
Old 06-05-2017, 06:59 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Code:
if [[ ! -f filename ]]; then
   echo "file not found"
   exit 1
fi
 
1 members found this post helpful.
Old 06-05-2017, 07:20 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
see "man test" also
 
Old 06-05-2017, 09:35 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
if [[ -f filename ]]; then
echo "Got it yo!"
else
{
   echo "Not yet dude" 
   exit 1
}
fi
that way you know either way.
 
1 members found this post helpful.
Old 06-06-2017, 06:55 AM   #6
dave@burn-it.co.uk
Member
 
Registered: Sep 2011
Distribution: Puppy
Posts: 601

Rep: Reputation: 172Reputation: 172
That's done your homework for you!!
 
Old 06-06-2017, 08:36 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

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

Introduction
 
Old 06-06-2017, 08:45 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,900
Blog Entries: 13

Rep: Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947Reputation: 4947
Quote:
Originally Posted by Waris View Post
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.
 
Old 06-06-2017, 08:54 AM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
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
 
Old 06-06-2017, 09:05 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 View Post
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.

Last edited by BW-userx; 06-06-2017 at 09:10 AM.
 
Old 06-06-2017, 10:11 AM   #11
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Original Poster
Rep: Reputation: Disabled
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!!!
 
Old 06-06-2017, 10:35 AM   #12
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 06-06-2017, 10:42 AM   #13
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Waris View Post
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.
Logical Not !
File test operators
Exit and Exit Status

Last edited by BW-userx; 06-06-2017 at 10:44 AM.
 
1 members found this post helpful.
Old 06-06-2017, 11:25 AM   #14
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Original Poster
Rep: Reputation: Disabled
Talking

Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 06-06-2017, 11:29 AM   #15
Waris
Member
 
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post

Thanks! This is pretty straight forward! gracias compadres!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 09:03 AM
Bash Scripting – Code Structure - Defining Multiple Points Of Entry In Bash Script carlr Programming 10 08-25-2014 03:38 AM
[To share Bash knowledge]Notes for Advanced Bash-Scripting Version 10 (Latest) jcky Programming 4 07-31-2014 10:24 AM
Defensive BASH Programming dugan Programming 10 05-31-2014 02:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration