nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

script on how to check if the file exists

This is a discussion on script on how to check if the file exists within the Shell scripting forums, part of the Development/Scripting category; Hi all, I am not really familiar with scripting. I just wanna ask a help from anyone on how to ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 06-07-2005, 02:01 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
Rep Power: 0
warren
Default script on how to check if the file exists

Hi all,
I am not really familiar with scripting.
I just wanna ask a help from anyone on how to check a file if it exists.
I have here my script on how i did it.

echo "checking MAKEDEV "
if [ ! `ls /dev/MAKEDEV` ]; then exit -1; fi
if [ ! `ls /etc/makedev.d/` ]; then exit -1; fi
if [ ! `ls /usr/sbin/mksock` ]; then exit -1; fi

This is just a simple, if the file exists then it will continue on next, if it does not exists then it will stop.
but the problem is if I want to find a lots of file, the tendency is I will type all of this file. Take for example i will check all the files on busybox if it exists, that would be a lot of time typing if and then.
My point here is i would like to create loop but i dont know.
Maybe i will just type ony:

/dev/MAKEDEV
/etc/makedev.d
/usr/sbin/mksock

and then it will loop to find this files, so saving so much time typing.
I hope somebody will help.
thanks so much, warren
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-07-2005, 06:06 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

You can do that as follows:
Code:
files="/dev/MAKEDEV  /etc/makedev.d  /usr/sbin/mksock"
# $i will hold single file at a time in following loop
for i in $files 
do
   if [ ! -f $i ]; then
     exit 1
   fi
done
Btw -1 is not accepted in bash as exit code...
Reply With Quote
  #3 (permalink)  
Old 06-07-2005, 06:23 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
My distro: Debian GNU/Linux
Posts: 482
Rep Power: 5
monk will become famous soon enough monk will become famous soon enough
Default

rockdalinux your code seems to correct , but it is only looking for file with -f option for directory you need to add -d option
[code]
files="/dev/MAKEDEV /etc/makedev.d /usr/sbin/mksock"
# $i will hold single file at a time in following loop
for i in $files
do
if [ ! -f $i -o ! -d $i ]; then
exit 1
fi
done
[code]
Reply With Quote
  #4 (permalink)  
Old 04-14-2006, 11:01 AM
Member
User
 
Join Date: Jun 2005
Posts: 45
Rep Power: 0
warren
Default

follow up question....
I have actually posted this for almost a year now.
Have patience to review it again.

thanks for this script.
the script will take the inputs from:
files="/dev/MAKEDEV /etc/makedev.d /usr/sbin/mksock"

To make my script smaller, I need to create a file which lists all the files that I need to find . Then my script will take the inputs from that file instead of typing it all in the script. How can i do that?

Thanks,
warren
Reply With Quote
  #5 (permalink)  
Old 04-14-2006, 06:51 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Replace line
Code:
files="/dev/MAKEDEV /etc/makedev.d /usr/sbin/mksock"
With following two lines:

Code:
INPUTFILE="/tmp/filelist.txt"
files="$(cat $INPUTFILE)"
It will read all filenames from /tmp/filelist.txt file and store list of files to files variable.
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
forced file check fsck smr36 Ubuntu / Debian 3 04-16-2008 10:23 AM
Shell script to check the disk space on remote systems vijayscripts Shell scripting 5 10-21-2007 07:29 PM
Command to Check Linux File System lacloai Getting started tutorials 3 05-14-2007 06:45 PM
shell script to open log files and check for faults trueman82 Shell scripting 1 11-23-2006 03:35 AM
freebsd ifconfig alias up ioctl siocaifaddr file exists erro ZigZag All about FreeBSD/OpenBSD/NetBSD 1 12-23-2005 05:49 PM


All times are GMT +5.5. The time now is 06:50 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36