Incorrect number of command line arguments and missing required files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Incorrect number of command line arguments and missing required files
# 1  
Old 05-22-2013
Incorrect number of command line arguments and missing required files

I am developing a script. This script takes in one parameter which is the name of a file whose content is a list of names of some files. The script can check whether those files exist in current directory.
Here is my question:
If the number of provided parameters is less than one or one of the files does not exist in the current directory, I have to print an information error message to standard error and abort the script with non-zero exit status. I know how to check the number of input arguments and whether some files exist in the current directory, but I don't know how to redirect the information error message to stand error. Anyone help?
Here is what I have done so far:
Code:
#!/bin/bash
if [ $# -lt 1 ] ; then #Check there is enough command line parameters.
        exit 1
fi

while read i
do #Check whether there is missing file
        if [ ! -e $i ] ; then
                echo "$i does not exist!"
                exit 1
        fi
done < "$1"
echo "All files exist!"
exit 0

Thanks in advance!
# 2  
Old 05-22-2013
The redirect to the standard is
Code:
echo "Error" 1>&2

so
Code:
#!/bin/bash
if [ $# -lt 1 ] ; then #Check there is enough command line parameters.
      echo "Not enough command line parameters" 1>&2
        exit 1
fi

if [ ! -e $1 ] ; then
		echo "$1 does not exist!" 1>&2
		exit 1
fi

while read i
do #Check whether there is missing file
        if [ ! -e $i ] ; then
                echo "$i does not exist!"
                exit 1
        fi
done < "$1"
echo "All files exist!" 
exit 0

This User Gave Thanks to franzpizzo For This Post:
# 3  
Old 05-22-2013
1 is default; you can do
Code:
echo "$i does not exist!" >&2

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to output a line missing a number

Ok, Lets see if I can explain this We have a script that pulls information from multiple files and outputs it, however I only need 2 Columns (of 11) from it right now I run the script like this: tkxtrn | awk '{print $5" "" "$9}' This gives me column 5 and 9, the only two I care for ... (5 Replies)
Discussion started by: shadowkraze
5 Replies

2. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

3. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

4. Shell Programming and Scripting

Need help with examine the number files in directories given as arguments

Hi , this is homework .. I have to finish it tomorrow, I did my best , but I found it so difficult .. Can You HELP Me ??! Write a bash shell script filestatic. The script should examine the number files in directories given as arguments (parameters) to this script. a. if one argument is... (1 Reply)
Discussion started by: abo-el-sos
1 Replies

5. UNIX for Dummies Questions & Answers

command line arguments

hi, can someone how to accept command line arguments as a variable using in script? like: ./scriptname arguments by accept arguments, I can use it in my script? thx! (1 Reply)
Discussion started by: ikeQ
1 Replies

6. UNIX for Dummies Questions & Answers

Command line arguments.

I am working on a script wherein i need the user to enter the Build ID for eg:the command line will show enter the build ID Now on entering the build ID it should be assigned to @ARGV. How can this be done.? (1 Reply)
Discussion started by: Varghese
1 Replies

7. Shell Programming and Scripting

command line arguments

-------------------------------------------------------------------------------- I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Discussion started by: skooly5
1 Replies

8. Shell Programming and Scripting

Opening Files as command line arguments

Hi have set a command line argument where the user enters the filename. I have set a condition that if the command line is null (user just enters return), the script will exit (with an error message). However, what if the user types something and the file isn't found. How will I go about... (20 Replies)
Discussion started by: Cactus Jack
20 Replies

9. UNIX for Dummies Questions & Answers

arguments in command line

Hi all, How many arguments can we pass while testing a prgm at command line.. I encountered an issue while passing 10 arguments. For $10 its taking argument passed for $1 followed by 'zero'. can we pass more than 9 arguments /Is there any other way. Thanks, rrs (6 Replies)
Discussion started by: rrs
6 Replies
Login or Register to Ask a Question