.sh script / Check for file in two directories then cat the files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting .sh script / Check for file in two directories then cat the files
# 1  
Old 07-27-2010
.sh script / Check for file in two directories then cat the files

Hi,

Here is what i'm trying to do,

-Check in two directories for a user inputed filename
-Then cat all the version found of the file to the current screen

I am a total nembie to programming, here what i have done so far....

Code:
#!/bin/bash
#Check in /home/loonatic and /var/named/master
#If the file exist cat it.
dir1="/home/loonatic/"
dir2="/var/named/master/"
if [ ls ${dir1} ${dir2} ${filename} ] ; then
do [[ -f $dir1/$filename ]] && cat "$dir1/$filename"
do [[ -f $dir2/$filename ]] && cat "$dir2/$filename"
else
exit

If you can point me toward a solution or reading material that would be great.

Thanks
# 2  
Old 07-27-2010
Try this:
Code:
#!/bin/bash

f="$1" #The filename is the argument of the script
[ -z "$1" ] && read -p'Input a file: ' f #No argument? We explicitly ask the user

DIR1="/home/loonatic/"
DIR2="/var/named/master/"

#while [ "$f" ]; do
	for d in "$DIR1" "$DIR2"; do
		[ -f "$d$f" ] && {
			echo "-> $d$f"
			cat "$d$f"
		}
	done
#	shift; f="$1"; done

exit 0

Usage: ./script [optional filename]
For the fun of it, uncomment the 2 commented lines to add support for multiples arguments: ./script file1 file2 file3...

Last edited by tukuyomi; 07-27-2010 at 12:33 PM.. Reason: added useless stuff =)
This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 07-27-2010
Thank you so much, you rock =)

I have a few scripts to build like that and you gave me a good fondation to start with.

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check current date file is created and with >0 kb or not for multiple directories

Hi All, I am new in scripting and working in a project where we have RSyslog servers over CentOS v7 and more than 200 network devices are sending logs to each RSyslog servers. For each network devices individual folders create on the name of the each network devices IP addresses.The main... (7 Replies)
Discussion started by: Pinaki
7 Replies

2. Shell Programming and Scripting

Shell script to check a file and delete old files

Hello, I needed help with a shell script where in it checks if a file exists under a directory and also checks the age of the file and delete it if it is older than 3 weeks. thanks (10 Replies)
Discussion started by: hasn318
10 Replies

3. Shell Programming and Scripting

File check script fails for multiple files

I want to check if any file with testing*.txt exists but my script fails if more than 1 file exists. It works fine for a single file if then echo "TEST21" fi -------------- bash: How do I fix this? Thanks Please use code tags next time for your code and data. (8 Replies)
Discussion started by: sumang24
8 Replies

4. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

5. Shell Programming and Scripting

Script for deleting files and directories when the file system reaches the threshold

Hi Can someone assist in writing a script. I have a filesystem named /sybase in my aix lpar. When this filesystem becomes 94% full all the files and directories under /sybase/logs should be deleted immediately. :confused: (7 Replies)
Discussion started by: newtoaixos
7 Replies

6. Shell Programming and Scripting

cat certain files in directories to files named after the dir?

Hi all, I have a directory with many subdirectories each named like so: KOG0001, KOG0002, ...KOG9999. Each of these subdirectories contain a variable number two kinds of files (nuc and prot) named like so: Capitella_sp_nuc_hits.fasta (nuc) and Capitella_sp_prot_hits.fasta (prot). The... (2 Replies)
Discussion started by: kmkocot
2 Replies

7. Shell Programming and Scripting

How to check if all directories of file's path exists?

I wonder if the script below is possible to write somehow more efficiently. It seems to me the problem is very common.. CreateFolders() # parameter: name of file with relative path with regard to directory $project_root { echo $1 | awk '{ n=split($1, array, "/"); ... (2 Replies)
Discussion started by: MartyIX
2 Replies

8. Shell Programming and Scripting

cat files in different directories

i have data files in different directories like /jadat /cadat etcc... i have list of this directories in a file files. content of files: ja ca directories: /jadat /cadat file in each of these directories natt_trans_like.dat i need to loop though each directory for a... (1 Reply)
Discussion started by: dsravan
1 Replies

9. Shell Programming and Scripting

Check all files in directories for string and remove.. possible?

Say I am in /var/adm/bin and I want to search through all the various scripts and such in that directory for a string, say, xxx@yyy.com I want to find every file with that in there and replace it with a single space. Is that possible? Or, is it possible to search every file and get a list... (7 Replies)
Discussion started by: LordJezo
7 Replies

10. UNIX for Dummies Questions & Answers

What/How to check before deleting files and directories

Hi there, I have a some directories containing web files that are old, and I need to remove them. I know that there might be sym links and hyperlinks pointing to these old directories. If that's the case, then I'll have to fix the links before deleting these old directories to avoid broken... (4 Replies)
Discussion started by: yvochan
4 Replies
Login or Register to Ask a Question