passing argument in script?

hi,
I want to implement some function to perform following task

if [ firstArgument is not provided ]; then
$TEXT = "Data_0"
else
$TEXT = $1
fi

if [ secondArgument is not provided]; then
$Lines = 45
else 
$Lines = $2
fi

Kindly suggest,

thanks
Pooja

$# return the number of argument.

1 Like

Which shell are you using?

In sh, dash, should run on bash as well:

#!/bin/sh

if [ ! "$1" ]; then
TEXT="Data_0"
else
TEXT="$1"
fi

if [ ! "$2" ]; then
Lines=45
else 
Lines=$2
fi
1 Like

Perfect, thanks..:slight_smile:

---------- Post updated at 03:15 PM ---------- Previous update was at 03:12 PM ----------

ummm, confusion..:confused::confused:
my firstargument is String and second argument is integer.

so what if I do following:

./script.sh text 
and 
./script.sh 10

I am just wondering, wont it take 10 as first argument???? in principle it should be my second argument..?? and vice versa..

Pooja

try this..

./script.sh text 10

Try as pamu says. To go further, you may need to quote your text if it contains spaces:

./script.sh 'text with spaces to be taken as first arg' integer_arg

Hi pamu,

I am confuse as I want to use it both ways:

./script.sh 10 

And here I want code to understand that it is secondArgument

and also like

 ./script.sh data_0 

where code should know that its firstArgument.

I hope, I stated it clearly now?

Thnaks
Pooja

This way maybe?

#!/bin/sh

#Default values
TEXT="Data_0"
Lines=45

if [ "$1" ]; then
	[ "$1" -eq 0 ] >/dev/null 2>&1; x="$?"
	if [ "$x" -lt 2 ]; then 	
		echo "Arg is an integer, to be set to Lines"
		Lines="$1"
	else
		echo "Arg is not an integer, so, it's probably some text"
		TEXT="$1"
	fi
fi

Hi tukuyomi,
Thanks for the reply !
As I understood it would work. But I need to provide both argument together. But yeah ECHO would help in recalling as what argument should be text and what should be integer?

Am I correct?
Pooja

I think you are very confused or i am confused :confused:

do you want to run one script multiple times and pass argument.

if you want to get return from the script as what type of argument is passed then just verify the string with numeric or characters..

./script.sh text
# here $1=text you can perform the operation here..

./script.sh 10
#here also $1=10

If i am guessing right you might talking about after running script 2 you want $2 as 10 right..?

That's not possible as per my knowledge.

hope it helps you..

and for checking string or integer check this...

$ [[ `echo "adsds" | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" || echo "itz not number"
itz not number

$ [[ `echo "1022" | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" || echo "itz not number"
itz number

$ [[ `echo "10df22" | sed 's/^[-+0-9][0-9]*//' | wc -c` -eq 1 ]] && echo "itz number" || echo "itz not number"
itz not number

You want your script to take

./script.sh text
./script.sh integer
./script.sh text integer

all three at the same time?
Or Am I confused as well? :s

Try this :

#!/bin/sh

#Define a function
intortext() {
	[ "$1" ] || return 0
	[ "$1" -eq 0 ] >/dev/null 2>&1; x="$?"
	if [ "$x" -lt 2 ]; then 	
		echo "Arg is an integer, to be set to Lines"
		Lines="$1"
	else
		echo "Arg is not an integer, so, it's probably some text"
		TEXT="$1"
	fi
}

#Default values
TEXT="Data_0"
Lines=45

#main()
intortext "$1"
intortext "$2"

If your question is how to use it with only parameter 2, use this:

 ./script.sh "" integer

And I would prefer if [ -z "$1" ]... instead of if [ ! "$1" ]...