LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2012, 02:30 PM   #1
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Rep: Reputation: Disabled
if [something] then ignore this if and go straight


I'm writing a bash script and I need to know something, there is a command for this?:
Code:
if [ -e "something" ]
then
skip this if and go up with the code
else
mkdir "something"
go up with the code
fi

...other code...
Thank you!

Last edited by Sbrizzu; 01-16-2012 at 03:06 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-16-2012, 02:42 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi Sbrizzu,

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.
 
Old 01-16-2012, 02:51 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The answer to your question is the null command, that is a colon, e.g.
Code:
if [[ -d something ]]
then
  :
else
  mkdir something
fi
but you can easily invert the logic using the not operator, that is an exclamation mark:
Code:
if [[ ! -d something ]]
then
  mkdir something
fi
Moreover, in this specific example (testing if something exists and is a directory, see -d) you can simply do
Code:
mkdir -p something
without the if/then construct, since the -p option causes mkdir to silently skip the creation of an existing directory. HTH.
 
3 members found this post helpful.
Old 01-16-2012, 02:52 PM   #4
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-16-2012, 02:54 PM   #5
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
The answer to your question is the null command, that is a colon, e.g.
Code:
if [[ -d something ]]
then
  :
else
  mkdir something
fi
but you can easily invert the logic using the not operator, that is an exclamation mark:
Code:
if [[ ! -d something ]]
then
  mkdir something
fi
Moreover, in this specific example (testing if something exists and is a directory, see -d) you can simply do
Code:
mkdir -p something
without the if/then construct, since the -p option causes mkdir to silently skip the creation of an existing directory. HTH.
Thank you! That's exactly what I mean!
 
Old 01-16-2012, 02:56 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
3 members found this post helpful.
Old 01-16-2012, 03:01 PM   #7
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
Since I'm here, can I ask another related question?
If I want to do that:
Code:
if [[ -e something/* ]]
then
rm -r something/*
execute the code in "else"
else
if you don't find it, simply skip the "rm -r" command 
...code...

Last edited by Sbrizzu; 01-16-2012 at 03:02 PM.
 
Old 01-16-2012, 03:09 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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

http://mywiki.wooledge.org/BashFAQ/004
 
Old 01-16-2012, 03:10 PM   #9
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

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
 
Old 01-16-2012, 03:15 PM   #10
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by T3RM1NVT0R View Post
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.

Last edited by Sbrizzu; 01-16-2012 at 03:26 PM.
 
Old 01-16-2012, 03:17 PM   #11
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
I simply deleted the if, as it was not necessary
 
Old 01-16-2012, 03:21 PM   #12
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

I think that will be easier. Instead of cp then you can cd to the directory and then execute wget.
 
Old 01-16-2012, 03:21 PM   #13
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
Ok, last edit, I managed like this, and it works:
Code:
if [[ -e ~/MyBook/Files/* ]]
then 
rm ~/MyBook/Files/*
else 
:
fi
 
Old 01-16-2012, 03:23 PM   #14
Sbrizzu
Member
 
Registered: Dec 2011
Distribution: Arch Linux
Posts: 40

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by T3RM1NVT0R View Post
I think that will be easier. Instead of cp then you can cd to the directory and then execute wget.
There is a wget option to do this instead of cd, is more simple, it's
Code:
-P
 
Old 01-16-2012, 03:26 PM   #15
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

That will work as well :-)
 
  


Reply



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
Why not straight answer? godoten Linux - Newbie 15 05-25-2011 04:49 PM
Install from ISO straight to HD ? NoobieDoobieDo Linux - Software 5 01-02-2011 08:28 AM
On Boot up: it goes straight through to XP liam_atkins Linux - Newbie 12 08-08-2005 03:49 PM
So let me get this straight... habala Linux - Newbie 5 01-05-2005 08:28 AM
can I go straight from 2.4-2.6 kernel? LavaDevil94 Linux - Software 4 04-03-2004 11:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 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