Variable substitution problem


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Variable substitution problem
# 1  
Old 05-13-2014
Variable substitution problem

HI

i was studying about variable substitution. below are info which was given in a online tutorial.

Code:
 
${parameter:-word}---> If parameter is null or unset, word is substituted for parameter.
          The value of parameter does not change.
${parameter:=word}---> If parameter is null or unset, parameter is set to the value of word.
${parameter:?message}--? If parameter is null or unset, message is printed to standard error. 
           This checks that variables are set correctly.
${parameter:+word}---> If parameter is set, word is substituted for parameter.
          The value of parameter does not change.

in order to understand its functinality i write a small script mentioned as below.
here i am not able to understand the use of
Code:
${parameter:?message}

and
Code:
${parameter:+word}

and why it is taking initial value of parameter which is "two" in last 2 cases.

Code:
 
$ cat subs
#!/bin/sh
#// ${parameter:-word}
echo ${parameter:-"this is one"}
echo "value of parameter ${parameter}"
echo next 
#//${parameter:=word}
echo ${parameter:="this is two"}
echo "value of paramer is ${parameter}"
echo next
#//${parameter:+word}
echo ${parameter:+"this is three"}
echo "value of parameter is ${parameter}"
echo next
echo ${parameter:? "this is three"}
echo "value of parameter is ${parameter}"
$

O/P
Code:
 
this is one
value of parameter 
next
this is two
value of paramer is this is two
next
this is three
value of parameter is this is two
next
this is two
value of parameter is this is two

# 2  
Old 05-13-2014
${var?message} will display the "message" when "var" is not set.
Code:
[root@it misc]# echo ${aVarNotSet?"aVarNotSet is not set"}
-bash: aVarNotSet: aVarNotSet is not set

${var+message} will substitute "message" if "var" is set, i.e., it will say that "var" contains "message" instead of the value that is stored in "var"
Code:
[root@it misc]# aVarNotSet="aVarNotSet is set"
[root@it misc]# echo ${aVarNotSet+"aVarNotSet is not set"}
aVarNotSet is not set
[root@it misc]# echo ${aVarNotSet}
aVarNotSet is set

As to your question, in the second case you are assigning the value to "parameter". So, now the "parameter" contains "this is two", and thus it is set.

In the third case it do not assign any value to "parameter", just substitute "this is three" instead of "this is two" and thus "parameter" remains unchanged.
In the last case it just checks is "parameter" is set, if not, prints the error message, "this is three". Since in this case "parameter" is set, you will not see the error message.
# 3  
Old 05-20-2014
Hi chaco193,

here i am not able to understatnd the practical use of variable substitution.

1. where and in which type of condition we can use these type ?

2. under which circumstances such type of substitution are helpful ?

3. why we use such type of substitution when we can use simple way like
Code:
x=5

may be my question looks to you silly but if you explain this to me with the help of some example ,this will really help me .

scriptor
# 4  
Old 05-20-2014
There is too many instances in creating scripts when we don't know the content of a variable. i.e. a variable gets set by another computation, or input from another process or user. Not always the result is acceptable or can be assumed.

Using those facilities, ensures that the minimal or acceptable defaults are available, perhaps to recover, instead of letting our script just fail, or fail us with bad results.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable substitution problem

Hi I am having a file like this ############################## j=1 while ] do temp_5=MODULE$j awk ' $1 ~ /'${!temp_5}'/ { do something }1' file1 > file2 ((j = j +1 )) done ################### Setting the variables like this (8 Replies)
Discussion started by: kshitij
8 Replies

2. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies

3. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

4. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

5. Shell Programming and Scripting

sed variable substitution problem

Hi, I am facing a strange problem. I have a script that used the following to search and replace text: sed 's/'"${find_var_parm}"'/'"${find_var_filter}"'/g' $ParmFile > $TempFile The values of $find_var_parm and $find_var_filter are set based on search criteria. The above seems to be working... (2 Replies)
Discussion started by: arsh
2 Replies

6. UNIX for Dummies Questions & Answers

Variable substitution

Hi, That might be pretty simple. How can I generate a variable name and get their value ? Thanks a lot. Something like: >CUSTOMER_NF=26 > object=CUSTOMER > echo ${object}_NF CUSTOMER_NF > echo ${${object}_NF} ksh: ${${object}_NF}: 0403-011 The specified substitution is... (7 Replies)
Discussion started by: Leo_NN
7 Replies

7. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

8. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies

9. UNIX for Dummies Questions & Answers

variable substitution

Hi everyone, I have a simple question to ask : In a script that I'm writting, I need to create variables on-the-fly. For instance, for every iterartion of the following loop a var_X variable should be generated : #!/bin/ksh a="1 2 3" for i in $a do var_${i}=$i echo "${var_$i}" done ... (1 Reply)
Discussion started by: ck-18
1 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question