Parameters/Args


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Parameters/Args
# 1  
Old 04-20-2011
Parameters/Args

Hello,

i have a problem. I must write a script, which wants 2 arguments.

for example:

PHP Code:
./test.sh x.txt y.txt 
it must be write x.txt in y.txt

and when i give 1 or no argument like

PHP Code:
/.test.sh x.txt 
must this give a error message like: SYNTAX <inputfile> <outputfile>

my solution is this:
PHP Code:
if [ "${1}]; then
 
echo args is"${1}"


else
 echo -
"SYNTAX <inputfile> <outputfile>"

if [ "${2}]; then
 
echo args is"${2}"


else
 echo -
"SYNTAX <inputfile> <outputfile>" 
but it does work, and i know that i do something false but what??

thanks
# 2  
Old 04-20-2011
you are not closing the if statement
Code:
if [ "${1}" ]; then
 echo args is: "${1}"
else
 echo -e "SYNTAX <inputfile> <outputfile>"
fi
if [ "${2}" ]; then
 echo args is: "${2}"
else
 echo -e "SYNTAX <inputfile> <outputfile>"
fi

or try this
Code:
#!/bin/ksh
if [[ ${1} -ne "" && ${2} -eq "" ]]; then
 echo args is: "${1}"
 echo -e "SYNTAX <inputfile> <outputfile>"
elif [[ ${1} -eq "" ]]; then
 echo -e "SYNTAX <inputfile> <outputfile>"
fi


Last edited by max_hammer; 04-20-2011 at 06:11 AM..
# 3  
Old 04-20-2011
Quick question here: why not just use cp?
This User Gave Thanks to pludi For This Post:
# 4  
Old 04-20-2011
Smilie I thought he want to do some other stuff Smilie
# 5  
Old 04-20-2011
thx but i write a script too, but it doesnt work fine.

PHP Code:
#!/bin/bash

touch $2

if [ -"$2" ];then
        
echo "$2 can be opened and write."


else
        echo 
"NO FILE OR RIGHTS TO OPEN!!"

fi


if [ "$2" ];then
        
echo saved in "$2"

else
        echo -
"SYNTAX: <IN> <OUT>"


fi


cat 
$> $
when i give 2 arguments then it works but when i give 1 or no argument, then gives it error messages.
1. error message is that touch operand is missing
2.error message is: line 26 : $2 : ambigous redirect


sorry i am a beginner Smilie
# 6  
Old 04-26-2011
Since you refer to a second parameter in your script, it needs 2 parameters to successfully run, but this isn't checked for anywhere. If you wrap it as below, then it will fail gracefully if the wrong number of parameters are given (none, one, or 3+).

Code:
if [ $# = 2 ]
then {
     your code here
}
else {
     echo -e "SYNTAX: $0 <IN> <OUT>"
}
fi;

If you want a different case for 1 parameter, then you can add another statement like if [ $# = 1] for 1 parameter, etc. or put it in a case of $# ($# is the number of parameters, if that isn't obvious from the example.)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Edit $args within a command

Hi, I'm using a while loop for a given command "bowtie2" with several parameters. mkdir clean paste <(ls --quote-name ./qc/sg_*_R1_val_1.fq.gz) <(ls --quote-name ./qc/sg_*_R2_val_2.fq.gz) |sed 's/"./-1 ./' | sed 's/gz"\t/gz\t -2 /' | sed 's/"//g' |\ while read args ; do ... (5 Replies)
Discussion started by: sargotrons
5 Replies

2. Shell Programming and Scripting

Store args passed in array but not the first 2 args

Store args passed in array but not the first 2 args. # bash declare -a arr=("$@") s=$(IFS=, eval 'echo "${arr}"') echo "$s" output: sh array.sh 1 2 3 4 5 6 1,2,3,4,5,6 Desired output: sh array.sh 1 2 3 4 5 6 3,4,5,6 (2 Replies)
Discussion started by: iaav
2 Replies

3. Shell Programming and Scripting

For Args and Nawk

I am trying to write a simple shell script that will take certain arguments (numerical values) and plug each one into a nawk command. I thought I would need to use for args x y z but i get syntax errors: for args 16 1 3 25 31 41 do nawk -F, '{if($10==$ &&... (8 Replies)
Discussion started by: he204035
8 Replies

4. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

5. UNIX for Dummies Questions & Answers

args of 50+ files

Hey.. I've gotten inspired by another thread and used this: #!/usr/bin/bash args 2,5 $(<file.list) It works but I'll like the results separated into different files or back into the 'used'/original files, whatever is easiest. e.g. use fileA | args 2,5 > fileB or fileA and then do this to... (9 Replies)
Discussion started by: lost
9 Replies

6. UNIX for Dummies Questions & Answers

command line args 2

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 and it printed the value of $1 after every number. I don't want that I just want... (2 Replies)
Discussion started by: skooly5
2 Replies

7. UNIX for Dummies Questions & Answers

command line args

I am trying to print command line arguments one per second. I have this while do echo "6" shift echo "5" shift echo "4" shift echo "3" shift echo "2" shift echo "1" shift done (2 Replies)
Discussion started by: skooly5
2 Replies

8. Programming

Command line args

My program usage takes the form for example; $ theApp 2 "one or more words" i.e. 3 command line arguments; application name, an integer, some text My code includes the following 4 lines: int anInteger; char words; sscanf(argv, "%d", &anInteger); sscanf(argv, "%s", &message); Based... (2 Replies)
Discussion started by: enuenu
2 Replies

9. UNIX for Dummies Questions & Answers

alias with args how to ...

Hello ( sorry newbie question ) I don't understand something im trying to make simple alias that takes 1 arg but it don't gives me the desire result here is what I have : stlist | awk '{print "ls -l "$2}' now I want to translate it to alias that takes instead of the $2 one arg so I did : ... (4 Replies)
Discussion started by: umen
4 Replies

10. Shell Programming and Scripting

Args to Array

Hello all, I have a question. Please help me to populate an array with the arguments passing to a Shell scripts. For example when I call "abc.sh a1 a2 a3" args (a1, a2, ...) recieved in an Array inside the abc.sh arr = a1 arr = a2 and so on... Thanks in advance, (2 Replies)
Discussion started by: Shaz
2 Replies
Login or Register to Ask a Question