[opensuse] BASH - I give up : (
Listmates, Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis? #!/bin/bash # This works [[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y' [[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" ) # This _doesn't_ work [[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" ) No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why? -- David C. Rankin, J.D.,P.E. | Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 510 Ochiltree Street | http://counter.opensuse.org/11.1/small Nacogdoches, Texas 75961 | Telephone: (936) 715-9333 | openSoftware und SystemEntwicklung Facsimile: (936) 715-9339 | http://www.opensuse.org/ www.rankinlawfirm.com | -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why? Because commands enclosed withing "( ..... )" get run in a subshell, where the
On Friday 14 November 2008 20:54:16 David C. Rankin wrote: the variable "verbose" is indeed set to "y" and then immediately discarded when the subshell exits. -- Jim
Jim Cunning wrote:
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why? Because commands enclosed withing "( ..... )" get run in a subshell, where the
On Friday 14 November 2008 20:54:16 David C. Rankin wrote: the variable "verbose" is indeed set to "y" and then immediately discarded when the subshell exits.
(..plink...light bulb goes on in a subshell..) Thanks! -- David C. Rankin, J.D.,P.E. | Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 510 Ochiltree Street | http://counter.opensuse.org/11.1/small Nacogdoches, Texas 75961 | Telephone: (936) 715-9333 | openSoftware und SystemEntwicklung Facsimile: (936) 715-9339 | http://www.opensuse.org/ www.rankinlawfirm.com | -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Friday 14 November 2008 20:54, David C. Rankin wrote:
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why?
What Jim Cunning said is correct. However, there is another similar kind of command grouping syntax that does not cause the creation of a sub-shell: the { curly brace }. Side-effect on local variables and shell options made within this construct _do_ persist outside / beyond it.
-- David C. Rankin
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Randall R Schulz wrote:
On Friday 14 November 2008 20:54, David C. Rankin wrote:
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why?
What Jim Cunning said is correct.
However, there is another similar kind of command grouping syntax that does not cause the creation of a sub-shell: the { curly brace }. Side-effect on local variables and shell options made within this construct _do_ persist outside / beyond it.
-- David C. Rankin
Randall Schulz
Randall, I will have to try that again. I thought something similar to brace expansion was correct, like awk '{ do this/that }', but the last time I tried (and I tried a lot), BASH kept complaining about my syntax (not a uncommon happening), I'll give it another go. My desire for one-liners for simple tests is to cut down on the amount of scrolling I have to do while editing which for me makes the script more readable. If I can reduce two pages of "does this variable exist"" "is it defined?" "is the file writable?"... to 5 lines, then I simply have a small test block that precedes the actual logic of the script instead of two pages of simple tests. Thanks for everyones help. It's getting wrapped around the axle on the little stuff that takes the most time... -- David C. Rankin, J.D.,P.E. | Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 510 Ochiltree Street | http://counter.opensuse.org/11.1/small Nacogdoches, Texas 75961 | Telephone: (936) 715-9333 | openSoftware und SystemEntwicklung Facsimile: (936) 715-9339 | http://www.opensuse.org/ www.rankinlawfirm.com | -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 15 November 2008 11:42, David C. Rankin wrote:
Randall R Schulz wrote:
On Friday 14 November 2008 20:54, David C. Rankin wrote:
Listmates,
Stuck on a stupid bash problem again. ...
What Jim Cunning said is correct.
However, there is another similar kind of command grouping syntax that does not cause the creation of a sub-shell: the { curly brace }. Side-effect on local variables and shell options made within this construct _do_ persist outside / beyond it.
-- David C. Rankin
Randall Schulz
Randall,
I will have to try that again. I thought something similar to brace expansion was correct, like awk '{ do this/that }', but the last time I tried (and I tried a lot), BASH kept complaining about my syntax (not a uncommon happening), I'll give it another go.
For whatever reason, if the closing brace is on the same line as the final command it encloses, you must end that command with a semicolon. This is not the case with parentheses.
My desire for one-liners for simple tests is to cut down on the amount of scrolling I have to do while editing which for me makes the script more readable. If I can reduce two pages of "does this variable exist"" "is it defined?" "is the file writable?"... to 5 lines, then I simply have a small test block that precedes the actual logic of the script instead of two pages of simple tests.
If you're worried about using undefined variables, there's an option that turns such references into errors (rather than the default empty substitution). % unset foo % echo $foo is bar is bar % set -u % echo $foo is bar bash: foo: unbound variable % set +u % echo $foo is bar is bar
......
-- David C. Rankin
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message -----
From: "David C. Rankin"
Randall R Schulz wrote:
On Friday 14 November 2008 20:54, David C. Rankin wrote:
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why?
What Jim Cunning said is correct.
However, there is another similar kind of command grouping syntax that does not cause the creation of a sub-shell: the { curly brace }. Side-effect on local variables and shell options made within this construct _do_ persist outside / beyond it.
-- David C. Rankin
Randall Schulz
Randall,
I will have to try that again. I thought something similar to brace expansion was correct, like awk '{ do this/that }', but the last time I tried (and I tried a lot), BASH kept complaining about my syntax (not a uncommon happening), I'll give it another go.
commands in braces need to be terminated by a linefeed or a semicolon before the close brace. { echo this echo is echo ok } { echo this ; echo is ; echo ok ; } { echo this ; echo is ; echo not } -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message -----
From: "David C. Rankin"
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why?
Because that is the very definition of () vs {} () creates a fully seperate child process. Same as if you had used a pipe or backticks or $() run another script by it's file name. Child processes inherit their environment from the parent. The parent gets to set up the env that the child will inherit, so the parent can set variables that the child will see. But the new process is a new, distinct process, it has it's own seperate environment, and except for the case of a parent spawning a child and creating that childs initial environment, no process can alter or read another processes environment. So the child process can't modify the parents environment. Nor can the parent further modify or even read the childs (there are exceptions, especially if we weren't talking about shell scripts, but for this question you may think of it as a rule or truth) {} can be used to group commands and to capture stdin/out/err ,yet have the commands still be running in the same process as the rest of the script, meaning they still share the same environment. Generally you only use () when you specifically require the effects if a seperate process, such as environment variables that won't affect the rest of the script, or the canonical example is to cd somewhere to do something and not have to worry about cd-ing back. cd from-dir ;tar cf - * |(cd to-dir ;tar xf -) For your example above you could just use {} to get what you want. That's exactly what {} is for. But really I would have used case instead of [[]] for that anyways, for two reasons. 1) case makes it easy to test for several things at once in a more convenient way than the [] tests. 2) case syntax obviates the need for {} to group commands to hinge off the same test. so it's like case was made for the task you are doing above. case "$1" in -v|--verbose) verbose=y ; echo "verbose mode" ;; esac -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
This doesn't really answer your question but skimming your email I assume you are trying to process command line options in bash scripts and so I'm wondering if you have considered using getopt or getopts to process your command line options instead? getopt is actually the older and "less good" way to do things but it's the one I use because I'm more familiar with it. The code below is a "getopt" example but if you google for "bash getopt" you will find much better examples of both getopt and getopts. ----- (beware of wrapping!) ----- useage () { echo " Useage: `basename $0` [-as] [-c FILE] [-i IP[:PORT]] [-u USERID] <DOMAIN> <DIRROOT> exit 1 } set -- `getopt "asc:u:g:i:" "$@"` || { useage exit 1 } while : do case "$1" in -a) # echo "option alias"; CALIAS=YES;; -s) # echo "option scripts"; SCRIPTS=YES;; -u) # echo "option userid"; shift; EMAILUSER="$1"; USERID="$1";; -g) # echo "option userid"; shift; GROUP="$1";; -c) # echo "option config"; shift; CONFIG="$1";; -i) # echo "option ip"; shift; IP="$1";; --) # echo "option --"; break;; esac shift done shift ----- -- John Lange www.johnlange.ca On Fri, 2008-11-14 at 22:54 -0600, David C. Rankin wrote:
Listmates,
Stuck on a stupid bash problem again. Why can't I set a variable within a set of parenthesis?
#!/bin/bash
# This works
[[ $1 == "--verbose" || $1 == "-v" ]] && verbose='y'
[[ $1 == "--verbose" || $1 == "-v" ]] && ( echo "this"; echo "that" )
# This _doesn't_ work
[[ $1 == "--verbose" || $1 == "-v" ]] && ( verbose='y'; echo "that" )
No matter what I do, if I try to set a variable inside the ( ) expression, it never gets set -- Why?
-- David C. Rankin, J.D.,P.E. | Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 510 Ochiltree Street | http://counter.opensuse.org/11.1/small Nacogdoches, Texas 75961 | Telephone: (936) 715-9333 | openSoftware und SystemEntwicklung Facsimile: (936) 715-9339 | http://www.opensuse.org/ www.rankinlawfirm.com |
-- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
John Lange wrote:
This doesn't really answer your question but skimming your email I assume you are trying to process command line options in bash scripts and so I'm wondering if you have considered using getopt or getopts to process your command line options instead?
getopt is actually the older and "less good" way to do things but it's the one I use because I'm more familiar with it. The code below is a "getopt" example but if you google for "bash getopt" you will find much better examples of both getopt and getopts.
Thanks for the examples John. Yes, I've used getopt and for dealing with more parameters I would, but for little short scripts I generally just do the handling with case or a few ifs or tests. This thread started that way with me just trying to shorten the command line parsing to a couple of lines like: [[ test ]] || (do this; do that) the parenthesis were screwing me my doing this and that in a sub-shell. What I needed and what I had tried to do was: [[ test ]] || {do this; do that;} but when I did it, I had forgotten to put in the trailing semi-colon after the last command.. That is where the learning needed to occur, and thankfully, it did ;-) -- David C. Rankin, J.D.,P.E. | Rankin Law Firm, PLLC | Countdown for openSuSE 11.1 510 Ochiltree Street | http://counter.opensuse.org/11.1/small Nacogdoches, Texas 75961 | Telephone: (936) 715-9333 | openSoftware und SystemEntwicklung Facsimile: (936) 715-9339 | http://www.opensuse.org/ www.rankinlawfirm.com | -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (5)
-
Brian K. White
-
David C. Rankin
-
Jim Cunning
-
John Lange
-
Randall R Schulz