Initialization error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Initialization error
# 8  
Old 07-12-2012
Quote:
Originally Posted by scriptor
${parameter:=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:

Code:
echo ${parameter:=word}

# 9  
Old 07-12-2012
Try like this, provided word has something already in it Smilie

Code:
${parameter:=$word}
echo $parameter

# 10  
Old 07-12-2012
Quote:
Originally Posted by PikK45
Try like this, provided word has something already in it Smilie

Code:
${parameter:=$word}
echo $parameter

This will not work and is potentially dangerous.
# 11  
Old 07-12-2012
Quote:
Originally Posted by scriptor
full script is

${parameter:=word}

echo $parameter

and below is the o/p

word: not found.
word
** ${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..
Code:
# ${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 )
Code:
# 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 )
Code:
# ${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
Code:
# 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.
Code:
# ${parameter:=$word}
ksh: 10:  not found.

** with echo
ksh and bash does not generate any errors and echoes the assigment results..
Code:
# echo ${parameter:=$word}
10

and the other one
Code:
# parameter=""
# word=20
# echo ${parameter:=$word}
20


regards
ygemici
This User Gave Thanks to ygemici For This Post:
# 12  
Old 07-12-2012
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
Code:
> 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

Code:
> var_subs   

khare$


var_subs[14]: vk: NO

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

Code:
> var_subs       

khare$


vai$
vai
vaibhav
vai

no idea why this is happening.

Last edited by Scrutinizer; 07-12-2012 at 07:07 AM.. Reason: code tags
# 13  
Old 07-12-2012
You need to read about variable expansion in korn before asking such questions.

Code:
${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.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error occurred during initialization of VM

Hi , I was invoking a sh file using the nohup command. But while invoking, I received a below error. Error occurred during initialization of VM Unable to load native library: /u01/libjava.so: cannot open shared object file: No such file or directory . Could you please help out. Regards,... (2 Replies)
Discussion started by: Kamal1108
2 Replies

2. Shell Programming and Scripting

Variable initialization

Hallo Team, I have a simple request and i would appreciate your help. I would like to use two dates in my script lets: A=$(date +"%d %B %Y") echo $A 23 June 2014 That's awesome now we cooking. Now i want B to be on the previous month eg: echo $B Should give me 23 May 2014 I would... (9 Replies)
Discussion started by: kekanap
9 Replies

3. Programming

C- static initialization of structures

Situation: I have an array of structures: struct my_struct_type { char tags_sn; char group_c; char is_err_c; }; struct my_struct_type stuff = { "abcd", 'A', 'E', "efgh", 'B', 'E', "ijkl", 'C', 'E' NULL, '\0', '\0' ... (14 Replies)
Discussion started by: garysk
14 Replies

4. UNIX for Advanced & Expert Users

openmpi - initialization failed

Hi everybody, my problem is the following: I'm trying to run an openmpi program on a cluster (atlasz.elte.hu, it's in hungarian, but you can try google translate), but I always got this error: Fatal error in MPI_Init: Other MPI error, error stack: MPIR_Init_thread(394)...........:... (0 Replies)
Discussion started by: jkobori
0 Replies

5. Shell Programming and Scripting

Declaring variables without initialization

I get an error in my shell script that line 1: )unexpected. Line 1 in my script (using sh by the way) is the variable I declared but did not initialize it. result= Is this wrong? How can I fix it? I am using the variable later in the program, so I figured I could just declare it first... (4 Replies)
Discussion started by: itech4814
4 Replies

6. Programming

Class Pointer initialization C++

Hello everyone, I have a question, that are the following ways of pointer intialization same ? ClassA *point; point = 0; point = new ClassA; Thanks a load in advance!! Regards, (10 Replies)
Discussion started by: mind@work
10 Replies

7. Programming

Char initialization

Hi All, char a="\0"; a) a contains \0 a contains garbage value b) a contains \ a contains 0 a contains garbage value Pls, let me know correct result is a or b. I guess a. Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

8. UNIX for Dummies Questions & Answers

Shell initialization files

As you know, when a user logs in, the shell reads the initialization files in an order something like below... ################### Bourne Shell /etc/profile > $HOME/.profile Bash Shell /etc/profile > $HOME/.bash_profile > $HOME/.bash_login > $HOME/.profile > $HOME/.bashrc C Shell... (3 Replies)
Discussion started by: SeanWuzHere
3 Replies

9. UNIX for Dummies Questions & Answers

Help regarding storing the initialization parameters

Hi all, In my shell script, I am reading some files, processing them, and writing the out-put in the log files. The out put contains number of rows in the file etc. I want to fetch the input files from a particular directory and I want to write the logs in a particular directory with a particular... (1 Reply)
Discussion started by: VENC22
1 Replies

10. Programming

Struct Initialization

Hi We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams. Following is a code sample: #include <stdlib.h> #include <stdio.h> typedef struct TEST3 {... (4 Replies)
Discussion started by: amatsaka
4 Replies
Login or Register to Ask a Question