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
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.
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:
or
I'm not sure I've gotten the full gist of what you are trying, but this should give you an idea or two.
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:
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 02:06 PM..
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
you can enter a complete sentence, so you need to parse through the variable. You can do this with something like:
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:
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.
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)
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)
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)
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)
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)
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)
--------------------------------------------------------------------------------
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)
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)
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)
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)