One shell / O/S I wandered onto had ksh set to nounset => reject reading nonexistent variables. None of my scripts worked! I had to turn that off quick in my .profile ! Talk about well know defaults, isn't "${some_name_never_set}" well known to be "" ?
... ... ...
According to the standards, if an equivalent of the command:
has been run, and an equivalent of the command
has not been run to counteract it, then:
Quote:
When the shell tries to expand an unset parameter other than the '@' and '*' special parameters, it shall write a message to standard error and shall not execute the command containing the expansion, but for the purposes of setting the '?' special parameter and the exit status of the shell the command shall be treated as having been executed and returned an exit status of between 1 and 125 inclusive. A non-interactive shell shall immediately exit. An interactive shell shall not exit.
Otherwise, ${unknown_variable} expands to an empty string.
No need to typeset, just unset or set x and test x for being set.
Somewhere in the man page as copies way below, ksh promises you can test just set and unset, no typeset, but I cannot see it, so maybe just go for blank or not. Your test cost is mostly variable lookup:
${parameter:-word}
If parameter is set and is non-null then substitute its value. Oth-
erwise substitute word.
word is not evaluated unless it is to be used as the substituted
string.
In the following example, pwd is executed only if d is not set or
is NULL:
print ${d:-$(pwd)}
If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.
${parameter:=word}
If parameter is not set or is null, set it to word. The value of
the parameter is then substituted. Positional parameters cannot be
assigned to in this way.
word is not evaluated unless it is to be used as the substituted
string.
In the following example, pwd is executed only if d is not set or
is NULL:
print ${d:-$(pwd)}
If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.
${parameter:?word}
If parameter is set and is non-null, substitute its value. Other-
wise, print word and exit from the shell , if the shell is not
interactive. If word is omitted then a standard message is printed.
word is not evaluated unless it is to be used as the substituted
string.
In the following example, pwd is executed only if d is not set or
is NULL:
print ${d:-$(pwd)}
If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.
${parameter:+word}
If parameter is set and is non-null, substitute word. Otherwise
substitute nothing.
word is not evaluated unless it is to be used as the substituted
string.
In the following example, pwd is executed only if d is not set or
is NULL:
print ${d:-$(pwd)}
If the colon ( : ) is omitted from the expression, the shell only
checks whether parameter is set or not.
Last edited by DGPickett; 05-08-2013 at 06:21 PM..
No need to typeset, just unset or set x and test x for being set.
Well, I'm in a function here, and I don't want to name clash, so I use typeset. The function is a widely-used utility function. I suppose I could follow some naming convention to help prevent clashes, but using typeset to create a local parameter seems cleaner. Preventing the name clash is my main reason for using typeset. Adding "-i" was kind of an afterthought to help performance, but the unexpected result on Version M 93t+ 2009-05-01 was that "-i" causes empty to show up as 0.
Name clashes will be. Does typeset do anything just assigning a value to the variable does not? All variables are local unless exported, and cannot escape their subshell in in any case. I assume shells keep unexported variables in a higher priority local container, to search before going to the exported flat ascii environment container.
Well, I'm in a function here, and I don't want to name clash, so I use typeset. The function is a widely-used utility function. I suppose I could follow some naming convention to help prevent clashes, but using typeset to create a local parameter seems cleaner. Preventing the name clash is my main reason for using typeset. Adding "-i" was kind of an afterthought to help performance, but the unexpected result on Version M 93t+ 2009-05-01 was that "-i" causes empty to show up as 0.
So, forget about -i and just use:
From this discussion, it should be clear that declaring a variable to have an integer value AND expecting it to be able to hold an empty string does not produce portable behavior.
If a function gets put on a pipe, all bets are off, anyway. If outside X is getting stomped by inside x, there is (), but I guess typeset is cheaper. It forces a local variable generation without interrogation of the two existing variable stores for variables to use.
Is it really maintainable long term even for you to have inside x and outside x floating around the same script?
For boolean, the -i is worth it only if you get c boolean with it like in 'x &&', '(( x || y ))' and such. I guess have 1 and 0 is familiar. I rarely use a boolean, maybe using flow instead.
Hello.
System : opensuse leap 42.3
I have a bash script that build a text file.
I would like the last command doing :
print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt
where :
print_cmd ::= some printing... (1 Reply)
How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address
and column 3 contains “cc” e-mail address to include with same email.
Sample input file, email.txt
Below is an sample code where... (2 Replies)
Hi,
Could anyone please shed some light on the following script lines and what is it doing as it was written by an ex-administrator?
cat $AMS/version|read a b verno d
DBVer=$(/usr/bin/printf "%7s" $verno)
I checked that the cat $AMS/version command returns following output:
... (10 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
Hi,
I have small script written in korn shell. When it is called from different script, its dumping core, but no core dump when we run it standalone.
And its not dumping core if we run the script using "/bin/sh" instead of "ksh"
Can some body please help me how to resolve this issue.
... (9 Replies)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)
Hi
how to check input is "empty" and "numeric" in ksh?
e.g:
./myscript.ksh k
output show: invalid number input
./myscript.ksh
output show: no input
./myscript.ksh 10
output show: input is numeric (6 Replies)