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
# 8  
Old 04-03-2012
Code:
function testfiles
{
        if [ "$#" -ne 1 ]
        then
                echo "Incorrect argument" >&2
                return 1
        fi

        if [ -z "$1" ]
        then
                echo "No file given" >&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

# 9  
Old 04-03-2012
There is some problem using the icons for some reason.
# 10  
Old 04-03-2012
I don't see any problem with the buttons myself, I suspect it's your browser.

You should post in the 'contact the admins' forum to contact the admins in any case instead of putting irrelevant content in a thread
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 04-03-2012
Quote:
Originally Posted by Corona688
Code:
function testfiles
{
        if [ "$#" -ne 1 ]
        then
                echo "Incorrect argument" >&2
                return 1
        fi

        if [ -z "$1" ]
        then
                echo "No file given" >&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

Why do you do a return 1 every time?
# 12  
Old 04-03-2012
Code:
iflBase=`ls -lrt *.base | awk 'BEGIN {ORS=" ";}; {print $NF}'`  testfiles $iflBase

@kristinu
Lost the plot here.
What is the code intended to do? Please show sample data and expected output and an overview of the process.
Please also post what Operating System and version you have and what Shell you are using today.

Also, your last post contains Microsoft funny characters.
# 13  
Old 04-03-2012
Quote:
Originally Posted by kristinu
Why do you do a return 1 every time?
Look closer -- I don't. I return a zero when it succeeds.

Shell error codes. 0 for success, nonzero for failure.

That lets you plug it right into an if-statement, the way it's meant to work.

Code:
if testfiles $filenames
then
        echo "filenames are okay"
else
        exit 1
fi

or even

Code:
testfiles $filenames || exit 1

# 14  
Old 04-03-2012
My browser started behaving strange for some reason. I am on Fedora release 12 (Constantine) Kernel \r on an \m (\l) file="" testfiles $file no file found testfiles /etc/passwd print nothing testfiles /etc/slartibartfast file does not exist testfiles 1 2 3 4 5 5 files found. Program requires only one input file
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