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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
06-05-2017, 05:53 PM
|
#1
|
Member
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40
Rep:
|
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!
|
|
|
06-05-2017, 06:03 PM
|
#2
|
Member
Registered: May 2017
Location: U.S.
Distribution: Un*x
Posts: 237
Rep:
|
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.
|
|
|
06-05-2017, 06:59 PM
|
#3
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Code:
if [[ ! -f filename ]]; then
echo "file not found"
exit 1
fi
|
|
1 members found this post helpful.
|
06-05-2017, 07:20 PM
|
#4
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
see "man test" also
|
|
|
06-05-2017, 09:35 PM
|
#5
|
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
|
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.
|
06-06-2017, 06:55 AM
|
#6
|
Member
Registered: Sep 2011
Distribution: Puppy
Posts: 601
Rep:
|
That's done your homework for you!!
|
|
|
06-06-2017, 08:36 AM
|
#7
|
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
|
of course I (we, others) have to take into account you may not know anything about programming whatsoever.
Introduction
|
|
|
06-06-2017, 08:45 AM
|
#8
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,900
|
Quote:
Originally Posted by Waris
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.
|
|
|
06-06-2017, 08:54 AM
|
#9
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
Quote:
Originally Posted by BW-userx
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
|
|
|
06-06-2017, 09:05 AM
|
#10
|
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
|
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
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.
|
|
|
06-06-2017, 10:11 AM
|
#11
|
Member
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40
Original Poster
Rep:
|
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!!!
|
|
|
06-06-2017, 10:35 AM
|
#12
|
Member
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40
Original Poster
Rep:
|
Quote:
Originally Posted by suicidaleggroll
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.
|
|
|
06-06-2017, 10:42 AM
|
#13
|
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
|
Quote:
Originally Posted by Waris
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.
|
06-06-2017, 11:25 AM
|
#14
|
Member
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40
Original Poster
Rep:
|
Quote:
Originally Posted by suicidaleggroll
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.
|
|
|
06-06-2017, 11:29 AM
|
#15
|
Member
Registered: May 2017
Distribution: Centos 6,7 and Windows
Posts: 40
Original Poster
Rep:
|
Quote:
Originally Posted by BW-userx
|
Thanks! This is pretty straight forward! gracias compadres!
|
|
|
All times are GMT -5. The time now is 06:27 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|