Initialization error

new to shell scripting.

below line is showing error in script.

${parameter:=word}

in the o/p first it shows the below error.

word: not found.
and then in next line print "word"

----------------

p2[10]: word:  not found.
word

---------------------------

OS is AIX and shell is KSH

Hello Scriptor, I would advise you to look into the basics once again :wink:

thx but can you please pin point the problem

Would you please post your complete script here?

From the above post what I interfere is,

 word 

should be a variable, you are missing "$" infront of this :slight_smile:

what is your full code or script ?
like this ?

# echo ${parameter:=word}

Does not seem to be any problem with that line. It'll help if you post some more lines before and after that line or the full script.

full script is

${parameter:=word}

echo $parameter

and below is the o/p

word:  not found.
word

This is the problematic line. After this variable expansion, parameter is set to the value "word" and then the shell will try to execute that command, which it can't find. I can't fathom why you need to use this. The following line will work:

echo ${parameter:=word}

Try like this, provided word has something already in it :slight_smile:

${parameter:=$word}
echo $parameter

This will not work and is potentially dangerous.

** ${parameter:=word}
### when you first execute that and if parameter is null or not set then ksh genaretes an error but assigns to "word" to parameter value..

# ${parameter:=word}
ksh: word:  not found.
# echo $parameter
word

### after that the first execute that command , then other executes ksh also genarete errors because of ksh executes for first argument ( ${parameter,,,,} ) so now parameter value's is "word",and returns the "word not found" error and then quit the executing process(shell internals)..

this same to that ( try this )

# word
ksh: word:  not found.

** ${parameter:=$word}
### when you execute this , ksh runs successfully and because of parameter and word value is null already.. (so any change and no error )

# ${parameter:=$word}
#

### first try below command , ksh returns same results because of ksh wants to execute word value and then returns the error normally but
parameter value assings to "10", because paramater value is NULL for first try or when parameter is null

# word=10
# ${parameter:=$word}
ksh: 10:  not found.
# echo $parameter
10

### other executes after than first , now parameter value was 10
and ksh tries to execute parameter value so "10" executed and then so ksh gives the error and quit from subsitution process in shell internal.

# ${parameter:=$word}
ksh: 10:  not found.

** with echo
ksh and bash does not generate any errors and echoes the assigment results..

# echo ${parameter:=$word}
10

and the other one

# parameter=""
# word=20
# echo ${parameter:=$word}
20

regards
ygemici

1 Like

thx ygemic for detail info. this really help me a lot.

below is the script which i am using now. but after the highlighted part(in red color) script does not execute. however if i commented the highlighted part it will run the rest part of the script.

below is my script

> cat -n var_subs
     1  #!/bin/ksh
     2  
     3  ################################${parameter:-word} - If parameter is unset, substitute "word"
     4  
     5  
     6   vk=
     7  echo $vk
     8  echo ${vk:-khare}$
     9  echo $vk
    10  ####################################################
    11  
    12  ### ${parameter:?word} - Display "word" or error if parameter is unset
    13  echo $vk
    14  echo ${vk:?"NO"}
    15  echo $vk
    16  
    17  
    18  
    19  ##############################################################
    20  
    21  ### ${parameter:=word} - If parameter is unset, set it to "word"
    22  echo $vk
    23  echo ${vk:=vai}$
    24  echo $vk
    25  ########################################################
    26  
    27  
    28  ### ${parameter:+word} - "word" is substituted only if parameter is set
    29  
    30  echo ${vk:+vaibhav}
    31  echo $vk

the o/p of the above script is

> var_subs   

khare$


var_subs[14]: vk: NO

the below is the o/p which i got after commenting the highlighted part.

> var_subs       

khare$


vai$
vai
vaibhav
vai

no idea why this is happening.

You need to read about variable expansion in korn before asking such questions.


${variable:-word}, ${variable-word}

This is expanded to the value of variable if it is set and not null, otherwise word is expanded. 
This is used to provide a default value if a variable is not set. 
The variable value remains unchanged.

-------

${variable:?word}, ${variable:?}, ${variable?word}, ${variable?}

This is expanded to the value of variable if it is set and not null, otherwise word is printed 
and the Korn shell exits. 
If word is omitted, 'parameter null or not set' is printed. 
This feature is often used in Korn shell scripts to check if mandatory variables are set.