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.
and i can make sure two of them was existens. aa is a file,dd is a directory .but the following code is not do well.
if[ -a "${aa}" ] ; then
echo "${aa} is a file"
elif [ -d "${dd}" ] ; then
ehco "${dd} is a directory "
fi;
the elif condition is always not running. anybody tell me why? thks
Last edited by qingyangzhang2@163.com; 03-22-2013 at 07:25 AM.
Reason: sorry ,i make a mistake .
- it's probably not a good practice to call your variable ("dd") exactly like a potentially dangerous tool (dd)
- You've got a lot of typos in the code above. I assume they are correct in your script. Please use the code tags when submitting any code in your posts.
- If the first condition is met (ie. the file exists), it is correct that it will never reach the second one (elif).
and i can make sure two of them was existens. aa is a file,dd is a directory .but the following code is not do well.
if[ -a "${aa}" ] ; then
echo "${aa} is a file"
elif [ -d "${dd}" ] ; then
ehco "${dd} is a directory "
fi;
the elif condition is always not running. anybody tell me why? thks
The -a (which is the same as the -e) tests to see if a file exists... and a directory is a file. If you wanted to see if a file was a regular file then use the -f test (True if file exists AND is a regular file).
Bash is not like HTML or CSS which ignores both extra spaces and lack of spaces. Your code is not properly formatted. Also, as the other person mentioned, dd is not a good choice for a variable, since it's an existing command/program and should be reserved for that.
Bash is not like HTML or CSS which ignores both extra spaces and lack of spaces. Your code is not properly formatted. Also, as the other person mentioned, dd is not a good choice for a variable, since it's an existing command/program and should be reserved for that.
...the elif condition is always not running. anybody tell me why? thks
The reason is pretty simple. If the if condition executes as true then else condition will not be executed. In your case, the if condition i.e. if[ -a "${aa}" ] is executing as true, thus else or elif condition isn't executing.
If you want to execute both conditions, do it like this:
Code:
#!/bin/bash
aa="/usr/local/tmp/test.txt"
bb="/usr/local/tmp/dd"
if [[ -a "${aa}" ]] ; then
echo "${aa} is a file"
if [[ -d "${bb}" ]] ; then
echo "${bb} is a directory "
fi
fi
Note: As per above discussions, use bb for storing directory name instead of dd.
The way you've used it in your script, it won't harm in any way. But it's always recommended to NOT use any executable command name in script, which is of no use actually, else it may be mis-interpreted and may cause problems to your system. A wrong use of this command may override the existing data on a disk device. So avoid using shell executables in scrips unless they're used in a proper and useful way. For more info. on dd, check it's manual here.
You can use bb instead of dd.
Last edited by shivaa; 03-23-2013 at 05:34 AM.
Reason: Typo
A bit about scripts - it is very useful to make them easy to read as that helps understand the logic used:
WARNING: pedantic mode on...
Take the original script:
Code:
#!/bin/bash
aa="/usr/local/tmp/test.txt"
dd="/usr/local/tmp/dd"
if[ -a "${aa}" ] ; then
echo "${aa} is a file"
elif [ -d "${dd}" ] ; then
echo "${dd} is a directory "
fi;
To make it more readable indent:
Code:
#!/bin/bash
aa="/usr/local/tmp/test.txt"
dd="/usr/local/tmp/dd"
if[ -a "${aa}" ] ; then
echo "${aa} is a file"
elif [ -d "${dd}" ] ; then
echo "${dd} is a directory "
fi;
This makes it clear that you want to distinguish between two things - a file, and a directory. It also shows that you don't specify what to do if neither condition is true (there is no "else"), but hints that more may be added...
Use of dd is discouraged not because it is wrong - but easily confused with the dd command. One reason it can be confused is because dd takes named parameters. A typical dd command is:
Code:
dd if=input conv=ebcdic of=output
And no, I don't think anybody uses EBCDIC anymore... But it points out that if the "if" (input file specification) is missed by a typographic error, you get an assignemnt...
Now lets take the next script:
Code:
#!/bin/bash
aa="/usr/local/tmp/test.txt"
bb="/usr/local/tmp/dd"
if [[ -a "${aa}" ]] ; then
echo "${aa} is a file"
if [[ -d "${bb}" ]] ; then
echo "${bb} is a directory "
fi
fi
This is even harder to scan... So lets indent:
Code:
#!/bin/bash
aa="/usr/local/tmp/test.txt"
bb="/usr/local/tmp/dd"
if [[ -a "${aa}" ]] ; then
echo "${aa} is a file"
if [[ -d "${bb}" ]] ; then
echo "${bb} is a directory "
fi
fi
This is easier to identify that the conditions are nested. It is also obvious that the logic isn't equivalent to the first example.
The underlying testing is on filesystem based items/objects. The -a test only identifies that something exists, it doesn't tell you what kind of object it is. filesystem objects are:
1. files - things that hold data
2. links - files that hold names of files in other locations
3. directories - files that hold file names (actually a file that is a list of inode number, name pairs)
4. devices - files that tie a name to a kernel structure for device input/output.
5. sockets - files that tie a bidirectional communications stream to a name.
6. fifo - files that tie a name to a pipe, a one way communication stream.
NOTE: there is a place where -a is used for "logical and", so using -a for file tests can be a bit ambigeous:
Code:
if [ $n -le 25 -a -a file ]; then
...
This is valid - most of the testing done for conditional expressions is handled not by bash, but by the "test" utility. The test utility uses -a for "and" and -e for "file exists" testing... Bash has a number of extensions, one of which is that much of the test utility is now built into bash. But confusion can result, so it would be better to use the -e for testing for the existence of a filesystem object.
Next, if you go through the man pages for bash (or easier, test), you will find that there is no explicit test for a device file... so the only way to identify a device file is to identify a special file first (as in ! -f , where the "!" is the not operator), then test for each of the listed special files...
Also remember, just because something exists doesn't mean you can read it. So there are access control tests available too.
The dd command is "data dump". It CAN destroy data on a disk when improperly used. But so can "cat".
or "disk destroyer" by many of fans of not using ANYTHING like dd, anywhere and why risk it?
Scripts have to be the opposite of risk, IMO.... so "run away" from anything even looking like 'dd'.
Your sanity will reward you.
Code:
if [ -d /usr/local/tmp/dd > 0 ]; then echo "Directory exists..." ; else echo "Directory does NOT exist." ; fi
if [ -f /usr/local/tmp/test.txt > 0 ]; then echo "File exists..." ; else echo "File does NOT exist." ; fi
1.5 hours on my Day Off working on someone else's code, how sick am I?
That "man bash" > ":/Conditional expressions" is a winner.
Last edited by Habitual; 03-23-2013 at 09:08 AM.
Reason: cleaned up -f for File from Directory in echos
or "disk destroyer" by many of fans of not using ANYTHING like dd, anywhere and why risk it?
Scripts have to be the opposite of risk, IMO.... so "run away" from anything even looking like 'dd'.
Your sanity will reward you.
Code:
if [ -d /usr/local/tmp/dd > 0 ]; then echo "Directory exists..." ; else echo "Directory does NOT exist." ; fi
if [ -f /usr/local/tmp/test.txt > 0 ]; then echo "File exists..." ; else echo "File does NOT exist." ; fi
1.5 hours on my Day Off working on someone else's code, how sick am I?
That "man bash" > ":/Conditional expressions" is a winner.
Must have sanity to write a good script-
Not sick at all Mate; just incredibly intelligent with a strong ambition to make right what's not complete yet.
Your not alone; I find myself fixing friends OS on my days off-
The reason is pretty simple. If the if condition executes as true then else condition will not be executed. In your case, the if condition i.e. if[ -a "${aa}" ] is executing as true, thus else or elif condition isn't executing.
If you want to execute both conditions, do it like this:
Code:
#!/bin/bash
aa="/usr/local/tmp/test.txt"
bb="/usr/local/tmp/dd"
if [[ -a "${aa}" ]] ; then
echo "${aa} is a file"
if [[ -d "${bb}" ]] ; then
echo "${bb} is a directory "
fi
fi
Note: As per above discussions, use bb for storing directory name instead of dd.
I understand that:
Quote:
aa="/usr/local/tmp/test.txt"
Is a file and I get that:
Quote:
bb="/usr/local/tmp/dd"
Is a directory; but what does the (-d) variable imply or is this actually a flag?
And is (fi) a way to indicate exit?
Ztcoracat :~$ Still studying BASH
Last edited by Ztcoracat; 03-23-2013 at 09:54 AM.
Reason: Not clear on bash
or "disk destroyer" by many of fans of not using ANYTHING like dd, anywhere and why risk it?
Because it depends on what you are doing. Disk recovery? dd. Forensics? dd. ISOs? dd.
Large data copies? dd (with a huge buffer...)
It is even used to backup partitions...
Quote:
Scripts have to be the opposite of risk, IMO.... so "run away" from anything even looking like 'dd'.
Your sanity will reward you.
No - scripts are good for recording exactly what you want to do... without forgetting steps. AND for getting exactly the right parameters and/or syntax right to work correctly.
Quote:
Code:
if [ -d /usr/local/tmp/dd > 0 ]; then echo "Directory exists..." ; else echo "Directory does NOT exist." ; fi
if [ -f /usr/local/tmp/test.txt > 0 ]; then echo "File exists..." ; else echo "File does NOT exist." ; fi
1.5 hours on my Day Off working on someone else's code, how sick am I?
That "man bash" > ":/Conditional expressions" is a winner.
Note that just because a directory does NOT exist, does NOT mean that the file doesn't exist - it might not exist, but then, the file might not be a directory either.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.