Fedora Linux Support Community & Resources Center
  #1  
Old 15th August 2009, 01:15 AM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Bash 101; “-a” inside test expressions

Been learning a little about the bash shell when I ran into the following problem. I have a directory “A” which has several directory in it, “B, C, and D”. I want to know when there is something in any of the sub directories B, C, or D ( a condition which changes from time to time.) I wrote the following script as a test for these conditions making use of file attribute checking within an “if” statement (as it was what I was learning at the moment):

Code:
 if [ \( -s $(ls ~/A/B/ ) \) ] && [\( -s $(ls ~/A/C/ ) \) ] && [ \( -s $(ls ~/A/D/ ) \) ]; then	

	echo "there is nothing in the folders"

else 

	echo "there is something in at least one folder"

fi
This appears to work just fine. !(Actually it does not work correctly - just took me awhile to realize it)! However, as I studied on I came to think that I should be able to get rid of the “] && [“ by replacing them with the inner test AND, “-a”, like so:
Code:
if [ \( -s $(ls ~/A/B/ ) \) -a \( -s $(ls ~/A/C/ ) \) -a \( -s $(ls ~/A/D/ ) \) ]; then	

	echo "there is nothing in the folders"

else 

	echo "there is something in at least one folder"

fi
The problem is, when I run this it does not work. WHY it does not work has me lost ... anyone??

Last edited by troyatlarge; 18th August 2009 at 10:57 PM.
Reply With Quote
  #2  
Old 16th August 2009, 01:45 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
OK - the book I have studied from is not so clear in all sorts of things - like what appears to be true of many write-ups when it comes to bash scripting. Anyway, after lots of searching the following guide seems pretty good covering the syntax for condition statements [ condition ], what you can an can not do inside them - so, if your like me and a little pissed about how this works via not getting it - this should help you out: http://www.linuxtutorialblog.com/pos...-if-statements
Reply With Quote
  #3  
Old 18th August 2009, 10:59 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Is there a simple way to do this???

This has taken me quite a long time to figure out (being a beginner) , and now that I have, maybe some can show me or at least lead me to better way to do the same thing.

I have in ~/ a directory “A” with many sub directories, B, C, D, etc., within it. I want to tell if anything at all is inside the sub directories B, C, D, etc. I did this via an if statement.

All the work happens, the way I found it anyway, via this:
Code:
ls -R ~/A/*/* 2>/dev/null | grep -n ^/ | grep ^1
Starting with ls -R ~/A/*/*, it list everything inside of the sub-directories B,C,D, etc., which are in A. However, if they are all empty it gives an error message which in the end trips up the test condition for my if statement, [ condition ]. As a result I toss any error message into the world of nothingness via

ls -R ~/A/*/* 2>/dev/null

This works ok inside the test so long as there is not more than one item inside the sub-directories. For some reason the test thing does not seem to like to deal with multiple files and directories in this way. Thus I needed to get the output to list one and only one item, regardless of how many are in there. I have piped the output into “grep -n ^/”, which numbers the output, the output being anything that starts with “/” (and everything does as ~/ expands into /home/...). That still has many listings, but each starts with a number so I turned it into one listing by running grep that starts with the number 1, of which there should be at most only one listing, thus “grep ^1”.

At this point [ -n “$(X)” ] , where X is the above piped commands, expands into one and only one thing, if something at all is in the sub-directories, or into nothing “”, if the sub-directories are empty, thus [ -n “$(X)” ] expands into a 0 or a 1 based upon anything being in the sub-directories, and thus useful for an if statement, such as:
Code:
#!/bin/bash
if [ -n "$(ls -R ~/A/*/* 2>/dev/null | grep -n ^/ | grep ^1)" ]; then  
	echo “something is in at least one folder”
else
        echo "the folders are empty"
fi
Now that seems like a lot to go through for what intuitively seems like it should be a very simple and basic thing to find out – is there stuff in those directories or is there not? My guess is that I have made it far more complex than needs to be – thus, if anyone knows a better way of going about all this please show me the simple way. Thanks
Reply With Quote
  #4  
Old 19th August 2009, 07:32 AM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Upon searching for a simpler way to do the above I discovered that the program does not notice if there are hidden files in the sub-directories. Looking further into "ls" I found that the -a option does not find them in the string
ls -aR ~/A/*/* . However, I did happen across the option -m which reduces all the greps out of the pipe for it makes the listing into one and only one entry (via separating everything by only a comma). thus the new work horse line is;
[ -n $(ls -mR ~/A/*/* 2>/de/null) ] But, it too suffers from never seeing hidden files. Any suggestions??
Reply With Quote
  #5  
Old 19th August 2009, 07:06 PM
php1ic Offline
Registered User
 
Join Date: Oct 2008
Location: London - England
Posts: 137
linuxfirefox
From what I understand, you want to be told if the subdirectories of A/ contain files, hidden or normal. I don't know if there is a simple way to do it with bash so I have done something similar to you and played with the output of ls

Code:
$ ls -aRF
.:
./  ../  A/

./A:
./  ../  B/  C/

./A/B:
./  ../  file_in_B

./A/C:
./  ../  .hidden
$
The "-F" option appends a "/" to directories allowing easy removal. My script then edits this output to remove everything that isn't a file then counts the remaining entires

Code:
#!/bin/bash
if [[ `ls -aRF A/ | sed '/^$/d' | grep -v '/' | wc -l` -gt 0 ]]
then
echo stuff
else
echo none
fi

exit 0
sed '/^$/d' removes blank lines
grep -v '/' prints lines that DO NOT contain a "/"
wc -l counts the number of lines

Anything left will be a file so the count will be zero if there are no file. Testing the script:
Code:
$ ./test.sh
stuff
$ rm A/B/file_in_B A/C/.hidden 
$ ./test.sh
none
$
If there are many subdirectories of A/ you may want to use -A instead of -a as an option for ls. The man page explains the difference.
Reply With Quote
  #6  
Old 22nd August 2009, 05:05 PM
troyatlarge Offline
Registered User
 
Join Date: Mar 2009
Location: Ely, NV. - 395 miles round trip to Wal-Mart
Posts: 333
linuxfedorafirefox
Hey, thank you so much. I thought I had actually tried the -ARF option to ls with negative results. Much to my surprise here you where having it work. I looked into ~/.bash_history to find what I did wrong only to discover by the time I had been playing with the -a and -A options I had placed the whole thing in a script and was changing the script instead of logging my efforts in ~/.bash_history : ( (~/.bash_history only shows a huge list of times I entered the scrip command, not what the script was actually running).

In any event I am happy to this it actually does in fact work (still not completely clear on -F, but will get there). With respect to sed (and awk) am only today turning to lessons on how to run them, what they do, etc. Thank you so very much for your help.
Reply With Quote
Reply

Tags
101, bash, expressions, inside, test, “a”

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
F12 Alpha test compose is ready now.please join us and execute > installation test Demz Fedora 12 Alpha, Beta & Release Candidates 28 27th August 2009 07:05 AM
What is inside the iso shotgun Using Fedora 2 22nd March 2006 07:10 PM
Inconsistent bash test behavior against aquota.user yafrank Using Fedora 0 21st August 2005 04:56 AM
Get bash completion working on fedora 3 (bash 3.0) madcat Guides & Solutions (No Questions) 2 27th November 2004 12:05 PM


Current GMT-time: 14:28 (Sunday, 21-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
...Hotel Galicja Travel Photos on Instagram - Summarecon Mall Serpong, Gading Serpong. Tangerang Photos on Instagram - Cojimies Travel Photos - Old Town Square Instagram Photos