nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-06-2005, 01:01 PM
Member
User
 
Join Date: Jun 2005
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 07-06-2005, 05:06 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 708
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
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 07-06-2005, 05:23 PM
monk's Avatar
Senior Member
User
 
Join Date: Jan 2005
Location: Tibet
OS: Debian GNU/Linux
Posts: 506
Thanks: 0
Thanked 8 Times in 6 Posts
Rep Power: 7
monk has a spectacular aura about monk has a spectacular aura about
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 14-04-2006, 10:01 AM
Member
User
 
Join Date: Jun 2005
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
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 14-04-2006, 05:51 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
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 Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
Reply


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 01:48 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38