Linux - NewbieThis 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.
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.
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.
-- 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.
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.
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.
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
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.