writing bash script to check if file present on that partition
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.
writing bash script to check if file present on that partition
Aim is to use basic commands found in all distros.
Currently I am using tiny core which uses busybox.
I am trying to see in root directory of every partition if they have a file named 'i-have-oxford-kundli'
Code:
mounted=$(df | grep -i 'sd'| awk '{ print $(NF) }')
echo mountpoints=$mounted
for word in $mounted; do
echo looking in=$word
found=$(ls -l $word| grep -i i-have-oxford-kundli)
if [ $? -eq 0 ]; then
echo $word has the desired file
echo over
else
echo not found
fi
done
I notice that 'over' is echoed for every mountpoint .
I want to run a set of commands when if condition is true not otherwise.
How can I do that ?
@sumeet inani: You might want to add the -P flag to the df command. This will make sure that all entries stay on the same line and aren't wrapped over 2 lines.
Your script basically does what it is supposed to do, which is the most important thing. With that in mind: It can be done more efficient. Have a look at this:
Code:
while read MOUNTED
do
echo "Looking in: $MOUNTED"
if [[ -a "$MOUNTED/i-have-oxford-kundli" ]]
then
echo "Found it."
else
echo "Not found."
fi
done < <(df -P | awk '/sd/ { print $(NF) }')
suicidaleggroll already mentioned -f (is it a regular file), -a (file exists) can also be used.
I will try your alternatives . My question was that : the second statement following 'then' got executed even when condition evaluated to false . How can that happen ?
for word in $mounted ; do echo looking in=$word found=$(ls -l $word| grep -i i-have-oxford-kundli)
if [ $? -eq 0 ] ; then echo $word has the desired file echo over else echo not found fi
done
Code:
$ ./kundli
mountpoints=/boot /dc /mnt/Win8 /mnt/Backup /media/Madhu_USB
looking in=/boot
not found
looking in=/dc
not found
looking in=/mnt/Win8
/mnt/Win8 has the desired file
over
looking in=/mnt/Backup
not found
looking in=/media/Madhu_USB
/media/Madhu_USB has the desired file
over
I see no wrong in the script. 'over' is only echoed on the partitions that has the 'i-have-oxford-kundli' file.
Edit: Just noticed that if you have LVM partitions, they dont show up by this script.
Last edited by Madhu Desai; 06-14-2013 at 09:10 AM.
enhancing your code a little, it can be further made to pattern-match only from first field, so that search wont pickup 'sd' or 'mapper' from any other field, just in case...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.