[SOLVED] if [something] then ignore this if and go straight
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
You are writing a bash script. I am unable to understand why you want to skip the first. I mean here is the syntax for if statement:
Code:
if [condition]
then
execute
execute
execute
else
execute
execute
execute
fi
From the post as I can understand you want that if the condition matches then do not execute the if part rather execute the else part. Any particular reason?
If you could let us know for which thing you are writing the script. What is the objective and what is the outcome that your expecting then it will be easy to guide you on that.
Ok, well, this script is intended to download each day a file from a site, and then put that file in folder, wich name's like "Monday 16", and that folder is in another folder, like "January 2012".
Some lines of code are better than words, so here it is that part, I want to substitute "continue" with a command that simply let me to skip the whole if and go straight:
Code:
datamonth=$(date +'%Y %B')
dataday=$(date +'%A %d')
if [ -e ~/MyBook/Edizioni/"${datamonth}" ]
then
continue
else
mkdir ~/MyBook/Edizioni/"${datamonth}"
fi
if [ -e ~/MyBook/Edizioni/"${datamonth}"/"${dataday}" ]
then
continue
else
mkdir ~/MyBook/Edizioni/"${datamonth}"/"${dataday}"
fi
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.
The basic logic seems clear to me. If the file doesn't exist, he has to go through some steps to create it first, or whatever. He's just a bit unclear on how to implement it.
To reverse the outcome of a test, use a "!".
Code:
if [ ! -e file ] ; do
touch file
done
Another option is to run the ":/true" command, which means basically to do nothing.
Code:
if [ -e file ] ; do
:
else
touch file
done
BTW, you can use "!" to invert the exit status of any command.
Code:
if ! grep -q "line" file ; do
Edit: Darnit! Take a few minutes to type everything up cleanly and get scooped by another poster.
Last edited by David the H.; 01-16-2012 at 02:58 PM.
No. I don't believe globs expand in tests like that. And even if they did, it would expand to all the filenames in the directory, and look something like this:
Code:
if [[ -e something/file1 something/file2 something/file3 ]]
...which would result in a syntax error. Globs only work inside [[..]] when doing string comparisons.
If you want to determine whether a directory is empty, see here:
Let me get it straight. Your script will download a file everyday. Later in your script you want that it should scan if month directory exists or not. So you basically want to scan /MyBook/Edizioni/ to see if that month folder exist or not if it does exist then it should skip rest of the code and directly come to creation of Date folder under month folder. If it does not exist then it should create a month folder and then go for creating date folder and then copy the file.
I think the following script should do:
Code:
ls -l /MyBook/Edizioni/"${datamonth}" 2> /dev/null
if [ $? = 0 ]
then
ls -l /MyBook/Edizioni/"${datamonth}"/"${dataday}" 2> /dev/null
if [ $? = 0 ]
then
cp downloaded_file /MyBook/Edizioni/"${datamonth}"/"${dataday}"
else
mkdir ~/MyBook/Edizioni/"${datamonth}"/"${dataday}"
cp downloaded_file /MyBook/Edizioni/"${datamonth}"/"${dataday}"
fi
else
mkdir ~/MyBook/Edizioni/"${datamonth}"
mkdir ~/MyBook/Edizioni/"${datamonth}"/"${dataday}"
cp downloaded_file /MyBook/Edizioni/"${datamonth}"/"${dataday}"
fi
Let me get it straight. Your script will download a file everyday. Later in your script you want that it should scan if month directory exists or not. So you basically want to scan /MyBook/Edizioni/ to see if that month folder exist or not if it does exist then it should skip rest of the code and directly come to creation of Date folder under month folder. If it does not exist then it should create a month folder and then go for creating date folder and then copy the file.
I think the following script should do:
Code:
ls -l /MyBook/Edizioni/"${datamonth}" 2> /dev/null
if [ $? = 0 ]
then
ls -l /MyBook/Edizioni/"${datamonth}"/"${dataday}" 2> /dev/null
if [ $? = 0 ]
then
cp downloaded_file /MyBook/Edizioni/"${datamonth}"/"${dataday}"
else
mkdir ~/MyBook/Edizioni/"${datamonth}"/"${dataday}"
cp downloaded_file /MyBook/Edizioni/"${datamonth}"/"${dataday}"
fi
else
mkdir ~/MyBook/Edizioni/"${datamonth}"
mkdir ~/MyBook/Edizioni/"${datamonth}"/"${dataday}"
cp downloaded_file /MyBook/Edizioni/"${datamonth}"/"${dataday}"
fi
Oh yes, if the download process was so simple maybe, the problem is that I have to work on files to obtain the link, and the code is, although not very long, quite long, and I wget directly in the right folder. I can simply copy-paste the download code in the else statement, and that's probably what I'm going to do.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.