Opening Files as command line arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Opening Files as command line arguments
# 15  
Old 01-23-2008
Thanks kpearson, I think I see the problem. The thing is as I am in effect opening a file from read variable so that I can work my program on it, I can't modify variable because my program won't be able to take it in. E.g. a file might be called "file 1" and so removing the space would change the filename to something that doesn't exist.

Therefore I just want to leave a condition preventing names like "file 1" from being entered. This would prevent any malfunction in my program. I have tried what you have written at the bottom. I noticed one of your back quotes are missing. Did u mean:

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

where filename is the line input.

Last edited by Cactus Jack; 01-23-2008 at 03:00 PM..
# 16  
Old 01-23-2008
Yes, 2 backtics are necessary. Sorry.
# 17  
Old 01-23-2008
You should remember about the following:
1. Always use " or ' for strings/parameters when you use them as value
2. Always use {} for variables - it helps to avoid problems like:
echo $10

If you are using filenames passed as command line params you could probably use the following (this is a simplified form that removes the list after use):
while [[ $# -gt 0 ]] ;do
do something on ${1}
shift
done
# 18  
Old 01-23-2008
Hi guys thanks for the help.. I think I have sorted it now using a pattern matching condition which looks for " " (spaces) and if any are present from the user input, an error is returned and the file is exited.

I thought there would be a pre-defined condition for this file scenario and that's why I asked about it.

Thanks for the reminder adderek. Can I ask you a quick question.

What does this do:

Code:
 function "$variable1" $variable2

Does the double quotes tell function to treat $variable1 as a string variable? I.e take in all the text in it as one value?
# 19  
Old 01-23-2008
function "$variable1" $variable2
This tells that there is at least 1 parameter and $variable2 can contain parameters 2-d and other.

Some example written ad-hoc (on files Smilie ):
Code:
#!/bin/ksh
function print_filenames {
while [[ $# -gt 0 ]] ;do
print "Filename: <${1}>"
shift 1
done
return 0
}
print_filenames "/home/user/First filename within $HOME.txt" '/home/user/Second filename within $HOME.txt' /home/user/Third filename within $HOME.txt

Output:

Filename: </home/user/First filename within /home/user.txt>
Filename: </home/user/Second filename within $HOME.txt>
Filename: </home/user/Third>
Filename: <filename>
Filename: <within>
Filename: </home/user.txt>
# 20  
Old 01-23-2008
Quote:
Originally Posted by adderek
function "$variable1" $variable2
This tells that there is at least 1 parameter and $variable2 can contain parameters 2-d and other.

Some example written ad-hoc (on files Smilie ):
Code:
#!/bin/ksh
function print_filenames {
while [[ $# -gt 0 ]] ;do
print "Filename: <${1}>"
shift 1
done
return 0
}
print_filenames "/home/user/First filename within $HOME.txt" '/home/user/Second filename within $HOME.txt' /home/user/Third filename within $HOME.txt

Output:

Filename: </home/user/First filename within /home/user.txt>
Filename: </home/user/Second filename within $HOME.txt>
Filename: </home/user/Third>
Filename: <filename>
Filename: <within>
Filename: </home/user.txt>
Ah I wasn't sure what you meant by "parameters 2-d" but your example illustrated it perfectly Smilie. My hats off to you.. that one is definitely going in my private notes Smilie. Thanks a lot

CJ
# 21  
Old 01-24-2008
And thanks for adding
Code:
shift

which I haven't used much and had forgotten about.

Nice touch.
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