Opening Files as command line arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Opening Files as command line arguments
# 1  
Old 01-13-2008
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 defining that condition?

Thanks
# 2  
Old 01-13-2008
see the "test" man page for the various options available.

Code:
if [ -e $file ]

or
Code:
if [ -f $file ]

or
Code:
if [ -r $file ]

or in your case since you want the complement of these conditions:
Code:
if [ ! -e $file ]

or
Code:
if [ ! -f $file ]

or
Code:
if [ ! -r $file ]

# 3  
Old 01-13-2008
or -x to test if an executable file exists Smilie
# 4  
Old 01-13-2008
Thanks for the help guys.. after viewing the man "test", I have come up with this:

if test -z $file -o ! -a $file

the -z bit is for when the user simply hits return and doesn't enter anything at the command prompt

the ! -a bit is if the file does not exist.

I have tried the above and it causes some problems:
- it doesn't catch the error if the user just hits return (i.e. enters nothing)
- if 2+ words are entered the above doesn't catch it

Valid files are allowed to proceed.

Any suggestions are welcome Smilie

thanks
# 5  
Old 01-13-2008
check for number of arguments?
Code:
echo $#
if [ $# -eq 0 ];then
 echo "No arguments "
fi

# 6  
Old 01-13-2008
Code:
if [ $# -eq 0 ]
then
    echo "Exiting.  No filename argument entered"
    exit 1
fi

if [ $# -gt 1 ]
then
   echo "Exiting.  Expecting a single filename argument"
   exit 1
fi

if [ ! -e $1 ]
then
   echo "Exiting.  File does not exist."
   exit 1
fi

if [ ! -f $1 ]
then
   echo "Exiting.  File is not a regular file"
   exit 1
fi

# 7  
Old 01-14-2008
Quote:
Originally Posted by fpmurphy
Code:
if [ $# -eq 0 ]
then
    echo "Exiting.  No filename argument entered"
    exit 1
fi

Thanks for the help. I have tried what fpmurphy has written (excluding the regular file test) but every time I enter a valid filename of a file that exists, I keep getting the "Exiting. No filename argument entered" error.

In fact no matter what I type, the above condition catches it. Any idea why that is?

if I remove the above and just leave the other conditions, non-existent files that have a single filename are caught but entering nothing doesn't and entering two worded filenames still doesn't get caught.

The $# conditions ain't working as they should. the ! -e $file is the only condition working properly.

This is puzzling! the $file is read and I enter the conditions directly after it. :S

Last edited by Cactus Jack; 01-14-2008 at 10:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop with command line arguments

Ubuntum, Bash version: 4.3.46 Hi, how can I create a loop where the command line arguments change (increase) and every time the number of arguments is different ? ### I have many gene names... that mean gene1=$2, gene2=$3, ...... geneN=$N+1 ### some time the number of gene is 25, other... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: Ray Sun
2 Replies

3. Shell Programming and Scripting

Need help to read command line arguments?

I am new to schell scripting . My objective is to write a ksh shell script that performs following tasks: - 1. Script reads all command line arguments (arguments are file names) and checks if it contains charachters "abc" in it. 2. If it contains "abc" it will execute a binary file xyz <command... (3 Replies)
Discussion started by: acmilan
3 Replies

4. Shell Programming and Scripting

command line arguments

hi,,,, I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
Discussion started by: vins_89
2 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

i want to list all command line arguments except

Hi all i want to list out all command line arguments except $1 i have passed to a script. Ex: sh cmdline.sh one two three four five o/p: two three four five (3 Replies)
Discussion started by: naree
3 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

10. Programming

command line arguments

Hi How to pass multi line text as a command line argument to a program. (i.e) ./a.out hi this is sample 0 file1 where hi this is sample should be stored in argv 0 in argv and so on... (3 Replies)
Discussion started by: bankpro
3 Replies
Login or Register to Ask a Question