Opening Files as command line arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Opening Files as command line arguments
# 8  
Old 01-22-2008
Hi guys still no joy on this issue. It appears that anything with an integer test condition does not work. E.g.

if [ $# - gt 1 ]
then echo "Single filename argument expected"
exit 1
fi

String tests (-z -n etc) are working fine.

However I don't know how else to exit my script if more than one argument is entered at the command line prompt.

This is the error I get when I enter a multiple argument (e.g. "wqe t") in the command line prompt:

file: line 10: [: too many arguments - this points to if [ $# - gt 1 ]
file: line 15: test: wqe: binary operator expected this points to 'if test -z $file'
file: line 21: [: wqe: binary operator expected this points to 'if test -z $file'
cat: wqe: No such file or
cat: t: No such file or directory

Any suggestions on this will be most helpful.
# 9  
Old 01-22-2008
Quote:
Originally Posted by Cactus Jack
Hi guys still no joy on this issue. It appears that anything with an integer test condition does not work. E.g.

if [ $# - gt 1 ]
then echo "Single filename argument expected"
exit 1
fi
Not sure if that's a typo, but the - needs to be next to the gt like:

Code:
if [ $# -gt 1 ]; then

# 10  
Old 01-22-2008
oops good spot there.. the if [ $# - gt 1 ] error is now gone but it still doesn't work.

When I enter "one two" in the command line the condition does not get caught by if [ $# -gt 1 ] and I get the following error:

file: line 15: test: one: binary operator expected - this points to 'if test -z $file'
file: line 21: [: one: binary operator expected - this points to 'if [ ! -e $filen ]'
cat: one: No such file or directory
cat: two: No such file or directory

When I enter a single filename argument the string test conditions work.. which leads me to think that integer tests won't work on a string variable thats read in.

I have 'read input' and then the testing conditions directly after it. Don't understand what the problem is. Smilie
# 11  
Old 01-22-2008
Are you calling your script this way?

Code:
./script arg1 arg2 (2nd arg just for testing)

or are you running the script then using

Code:
read FN

to get entries after the script is running?

The suggestions above are all based on the first scenario, not the second.

If you are using $#, you could then assign, or only assign, $1 to to the file variable after verifying that $# = 1 as it should for a single passed command-line variable.

Ie:

Code:
#!/bin/sh

if [ $# = 1 ] ; then
  :
else
  echo Incorrect number of command-line parameters
  exit 1
fi

FN=$1

[rest of script using $FN as passed parameter]
...

or

Code:
#!/bin/sh

CHK=$1

if [ 1"$CHK" = '1' ] ; then
  echo -n "Input parameter : "
  read FN
fi
...

I'm not sure I've gotten the full gist of what you are trying, but this should give you an idea or two.
# 12  
Old 01-23-2008
Tools

I am not sure if I understand the issue correctly. But you might try this one:

#!/bin/ksh
# We expect only one command line argument
[[ $# -gt 1 ]] && { print -u 2 -- "You should specify only a single argument - path to an non-empty file.";return 1; }
[[ -s "${1}" ]] || { print -u2 -- "File specified is empty!";return 1; }
[[ -r "${1}" ]] || { print -u2 -- "File specified is not readable!";return 1; }

# Now you could whatever you wish with the file - it should exist, should be readable and non-empty
# 13  
Old 01-23-2008
Hello sorry for the confusion. My program script basically takes in a file and performs actions on it. The main function of the program takes in the filename and a few other variables. The program works correctly for single filenames (e.g. file1) but causes problems with multiples (e.g. file 1)

I think the best way to describe it is by example:
Code:
#!/bin/bash

echo "Enter a filename:"
read filename

if test -z $filename
then
        echo "No filename entered. Please try again. "
        exit 1
fi

if [ ! -e $filename ]
then    echo "File does not exist. Please try again."
        exit 1
fi

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

echo "The filename is "$filename

Now when I run this script and enter "file1" (file1 exists) the program works. But when I type "file 1" (which also exists) I get this:

Enter a filename:
file 1
program: line 8: test: file: binary operator expected
program: line 14: [: file: binary operator expected
The filename is file 1


As you can see my script won't work correctly when multi filename arguments are typed in by the user and I want to create a condition to stop the script and warn the user if the he/she does that.

For some reason if [ $# -gt 1 ] doesn't work. From my testing, anything with $# doesn't work. I have tried if [ $filename -gt 1 ] but I get an extra error:
program: line 19: [: too many arguments

and it still doesn't catch the condition. Note that the first two conditions work with single filename arguments.

I hope clarifies my problem. My program works and I just want to add a working condition to ensure only single filename arguments are entered.

Any help on this will be most appreciated and thank you for your patience.

CJ

Last edited by Cactus Jack; 01-23-2008 at 01:06 PM..
# 14  
Old 01-23-2008
That makes much more sense. The $# is looking only for command-line parameters passed into the script. That's why it's not working.

When you do

Code:
read variable

you can enter a complete sentence, so you need to parse through the variable. You can do this with something like:

Code:
VAR=`echo $VAR|perl -pe 's/ //eg'`

which will remove all the spaces from the variable. That's probably not what you are after, but it's one way to make sure your variable doesn't have spaces in it, which is why you are getting the error.

Another thing you could do is test for the number of elements in the variable and if it's over 1, then fail. That's if you actually can't have spaces.

You might do that by using this top-of-my-feeble-mind thought:

Code:
CHK=echo $VAR|cut -f2 -d' '`
if [ "$CHK" -ne '' ] ; then
  echo "Multiple filenames entered. Retry required."
  exit 1
fi

Again, I'm not sure you are after such a thing as your example showed file 1 as valid, which probably shouldn't be and if it is, you would need to enter the file name as file\ 1 rather than just file 1.

My 2 bits.
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