Apparently this is a difference between bash-3 and bash-4.
(I reproduced the old behavior with bash-3.2.57 and the new behavior with bash-4.1.)
You are right, local var without a value should be a declaration as in bash-4, rather than a definition as in bash-3.
I'm used to use local in all my bash functions just to make sure I'm not messing with variables from the parent script. My workaround to make scripts compatible with bash-3 and bash-4 is :
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
local var
unset var
echo "var is ${var+set}${var-not set}"
}
f1
Do you see any better idea?
Thx
This User Gave Thanks to chebarbudo For This Post:
Thanks MadeInGermany, I know :+ and :- very well.
In my particular case, I actually need to know if a var is set. Example: In a function, I need to parse an argument and at the same time tell if it matches a regular expression.
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
local user domain
unset user domain
eval $(
echo "$1" | sed -rn 's/^([a-z0-9.]+)@([a-z0-9.]+)$/user=\1;domain=\2/p'
)
if [[ ${user+set} = set ]]; then
echo "this is an email address. the usename is $user. the domain is $domain"
else
echo "$1 is not an email address"
return 1
fi
}
f1 "$@"
foo:~# bash /tmp/test me@mail.com
this is an email address. the usename is me. the domain is mail.com
foo:~# bash /tmp/test dummy
dummy is not an email address
Of course this is a bare simple example and it's not accurate. But it tells you my original purpose. In the case of bash-3.x, the script fails if you remove the unset command.
Ok my bad example, sometimes I need the following...
Code:
foo:~# cat /tmp/test
#!/bin/bash
f1() {
local file ext
unset file ext
eval $(
echo "$1" | sed -rn 's/^([a-z0-9]+)(\.[a-z0-9]+)?$/file=\1;ext=\2/p'
)
if [[ ${ext+set} = set ]]; then
echo "this is a file${ext:+ with extension }$ext."
else
echo "$1 is not a correct filename"
return 1
fi
}
f1 "$@"
foo:~# bash /tmp/test wei:rd
wei:rd is not a correct filename
foo:~# bash /tmp/test file
this is a file.
foo:~# bash /tmp/test file.ext
this is a file with extension .ext.
The above example would not work with [[ ${ext:+set} = set ]].
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)
This really puzzles me. The following code gives me the error 'expr: syntax error' when I try to do multi-line comment using here document
<<EOF
echo "Sum is: `expr $1 + $2`"
EOF
Even if I explicitly comment out the line containing the expr using "#", the error message would still exist... (3 Replies)
Hi,
I'm trying to sort a text file "test":
S12
S_S12
S_S1_12
S15
S_N15
S_N1_15
By "sort test", I get:
S12
S15
S_N1_15
S_N15
S_S1_12
S_S12
It seems weird:
Comparing Line 2 and Line 3, it must be that '-' is bigger than '1'; however, comparing Line 3 and Line 4, it seems that... (3 Replies)
I've posted about this before, but only recently narrowed the problem down to a specific cause.
Ok, first of all, the behavior:
It occurs when autocompletion brings up its list (not when there is only a single option). Basically, if I were to type, say,
cd ~/<TAB>
I would get something... (2 Replies)
I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this:
$ look "string" "/home/patrick/filename.txt"
However, this gives me the following message:
"look: /home/patrick/filename.txt: File too large"
So, I have two... (14 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)
Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option?
For example, if I have the example script (we'll name it ex.sh):
#!/bin/sh
if
then
echo "Bye."
exit 2
fi
And I execute it like this:
>./ex.sh
It... (6 Replies)