Search for file, give error if more than one file is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for file, give error if more than one file is found
# 1  
Old 04-03-2012
Search for file, give error if more than one file is found

Want to write a function that prints an error when passed a list of file names. If the file list is empty, print error "no file found", if there are more than one file, print "error more than one file found"
# 2  
Old 04-03-2012
Code:
function testfiles
{
        if [ "$#" -ne 1 ]
        then
                echo "Incorrect argument" >&2
                return 1
        fi

        if [ ! -f "$1" ]
        then
                echo "$1 does not exist" >&2
                return 1
        fi

        return 0
}

testfiles /etc/passwd

testfiles /etc/slartibartfast

testfiles 1 2 3 4 5

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-03-2012
I might pass an empty string. What should I do in this case?
# 4  
Old 04-03-2012
Depends what you want it to do.
# 5  
Old 04-03-2012
I might pass an empty string. What should I do in this case?
Code:
iflBase=`ls -lrt *.base | awk 'BEGIN {ORS=" ";}; {print $NF}'`  testfiles $iflBase


Last edited by methyl; 04-03-2012 at 05:42 PM.. Reason: code tags
# 6  
Old 04-03-2012
I repeat: Depends what you want to do about an empty string.

What should it do about an empty string? error? default? other? what?
# 7  
Old 04-03-2012
Quote:
Originally Posted by Corona688
I repeat: Depends what you want to do about an empty string.

What should it do about an empty string? error? default? other? what?
error. If string is empty, it means that no file has been found, and specification of the file is mandatory. I got it. if [ $NF -eq 0 ]; then ... elif [ $NF -gt 1 ]; then ... fi

Last edited by kristinu; 04-04-2012 at 07:06 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK. If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the file is sent to the... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Bash - trap error file not found

Hello. In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort. Example ( part of script running with -x option set) : + return 0 + RETURN_CODE=0 + ] + /root/bin/200_yast_install/00_reset_yast_install bash:... (5 Replies)
Discussion started by: jcdole
5 Replies

3. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

4. AIX

ksh file not found error for various files across system

Our AIX system is currently having an issue where calls to various executables are returning errors such as ksh: /usr/bin/oslevel: not found. This is even happening with system files, third party scripts, and with the full path included. Telnet connections are also closing immediately upon... (6 Replies)
Discussion started by: Chthonic
6 Replies

5. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

6. Programming

Error found while opening any pdf file from IE 6

Hello All, My application is followed J2ee architecture. It contains Java and jsp codes. This application generates some reports in pdf format. I am using Weblogic 10.3 and jdk 6. While I want to open the pdf in IE 6 with service pack 2 , the pdf not showing properly. It comes in byte code.... (3 Replies)
Discussion started by: priyankak
3 Replies

7. Shell Programming and Scripting

file not found error during loop

Hi, I have a strange problem and I'm sure its because I'm doing something stupid. When I loop through all of the files in a directory they cannot be found. Any help would be greatly appreciated. Thanks, -E $ touch a.txt $ touch b.txt $ ls a.txt b.txt $ for f in `ls` ; do... (3 Replies)
Discussion started by: earls
3 Replies

8. Shell Programming and Scripting

source a property file in script give error

I have sourced a property file in my script like this to load some variables in the script Then i am able to echo all the property file values inside script but the script is not able to recognize other unix commands #!/bin/bash . build.properties mkdir build i am getting error ... (3 Replies)
Discussion started by: codeman007
3 Replies

9. Shell Programming and Scripting

search for a file, if not found sleep and repeat it for 3 hrs

Hi, Can someone help me with this script .... I need a ksh script, which will search for a specific file in a directory, if file not found sleep for 10 mins and search again, if still not found sleep again for 10 mins and so on .... it should search for that file for 3 hours and if that file... (5 Replies)
Discussion started by: santosham
5 Replies

10. Programming

Not able to compile Pro*c file due - give errors and points to /usr/include/.. file

I am trying to compile the pro*C file but gives errors. It says it encountered "std" while it was expecting ; , = ( $ $ORACLE_HOME/bin/proc tradewind/dataaccess/Blob.pcc Pro*C/C++: Release 10.2.0.3.0 - Production on Fri May 9 11:10:54 2008 Copyright (c) 1982, 2005, Oracle. All rights... (0 Replies)
Discussion started by: shafi2all
0 Replies
Login or Register to Ask a Question