Hi all,
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
if [ -n $# ] then WORDS=$1 else WORDS="these are some words" fi echo $WORDS;
The second case is always comes back "".
But if I write
WORDS='these are some words' echo $WORDS
I get the assigned string.
Why doesn't the assignment work when inside an if/then? How do I make it work? What's the difference between the case where the assignment is inside the if/then and outside the if/then?
TIA, Mike Wright
On 08/16/2014 02:44 PM, Mike Wright wrote:
if [ -n $# ] then WORDS=$1 else WORDS="these are some words" fi echo $WORDS;
The second case is always comes back "".
But if I write
WORDS='these are some words' echo $WORDS
I get the assigned string.
In your first example, you use full quotes (") but in your second, you use single ('). Try using single quotes and see if it makes a difference.
On Sat, Aug 16, 2014 at 02:44:14PM -0700, Mike Wright wrote:
Hi all,
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
if [ -n $# ] then WORDS=$1 else WORDS="these are some words" fi echo $WORDS;
The second case is always comes back "".
But if I write
WORDS='these are some words' echo $WORDS
I get the assigned string.
Why doesn't the assignment work when inside an if/then? How do I make it work? What's the difference between the case where the assignment is inside the if/then and outside the if/then?
This works:
#!/bin/sh if [ $# -ne 0 ] then WORDS=$1 else WORDS="these are some words" fi echo $WORDS
the hashbang line should be used, but doesn't actually change the output of this script.
"$#" is an integer, not a string, and that it should therefore be tested as an integer and not a string, hence the if statement being:
if [ $# -ne 0 ]
and you don't NEED the quotes around the literal string, though it doesn't harm to put them there, perhaps as documentation to make clear what you intention is.
On 08/16/2014 02:44 PM, Mike Wright wrote:
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
I found this example that might help: http://tldp.org/LDP/abs/html/comparison-ops.html#STRTEST
On Sat, Aug 16, 2014 at 02:44:14PM -0700, Mike Wright wrote:
Hi all,
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
if [ -n $# ]
This will always be true. -n tests if a string is empty or not. 0 counts as non-empty. You should use one of the comparison operators. Try something like this:
[ $# -gt 0 ]
Hope this helps,
On Sun, 2014-08-17 at 00:42 +0200, Suvayu Ali wrote:
On Sat, Aug 16, 2014 at 02:44:14PM -0700, Mike Wright wrote:
Hi all,
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
if [ -n $# ]
This will always be true. -n tests if a string is empty or not. 0 counts as non-empty. You should use one of the comparison operators. Try something like this:
[ $# -gt 0 ]
Hope this helps,
-- Suvayu
Open source is the future. It sets us free.
Are you looking for behavior that the following test script demonstrates?
#!/bin/bash
theArg=${1:-The Default Value}
echo $theArg
08/16/2014 04:14 PM, Mark C. Allman wrote:
On Sun, 2014-08-17 at 00:42 +0200, Suvayu Ali wrote:
On Sat, Aug 16, 2014 at 02:44:14PM -0700, Mike Wright wrote:
Hi all,
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
if [ -n $# ]
This will always be true. -n tests if a string is empty or not. 0 counts as non-empty. You should use one of the comparison operators. Try something like this:
[ $# -gt 0 ]
Hope this helps,
-- Suvayu
Open source is the future. It sets us free.
Are you looking for behavior that the following test script demonstrates?
#!/bin/bash theArg=${1:-The Default Value} echo $theArg
Thanx Mark,
Very elegant. I like it.
I was just going through the section on "variable substitution" in "Unix in a Nutshell" and saw that.
On 16Aug2014 14:44, Mike Wright mike.wright@mailinator.com wrote:
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
This is general shell stuff, not bash specific.
if [ -n $# ]
This is always true. Even "0" is a nonempty string.
Test [ $# -gt 0 ] instead.
WORDS=$1 else WORDS="these are some words" fi
I write this stuff like this:
# very near the top of the script words="these are some words"
[... command line parsing...] if [ $# -gt 0 ] then words=$1 shift fi
Tip: run your script with "-x":
sh -x my_script
It will show the actual commands executed.
Finally:
Never use $UPPERCASE names for variables that are local to your script. Use lower case for script-local variables.
Why?
- exported variables are by convention names with upper case, so using lower case makes it obvious that this variable is for your script versus general use (like $PATH)
- if a variable _is_ in the exported environment, and your script uses that name, the changed value will get exported to all the commands your script runs even if you don't export it yourself: it came in from the extrernal environment and it will automatically go out with the environment given to subcommands
- you can't expected (or be expected) to know every exported name that can possibly be used, nor even those commonly in use; by adopting the use of lower case names for script-local variable you entirely avoid needing omniscience about exported names.
You're writing the script with $WORDS probably because there are many many example scripts like it. They've been written by people who have never thought this through.
Cheers, Cameron Simpson cs@zip.com.au
Fatal error! Hit any user to continue... - Phillip Coles Phillip.Coles@tas.for.csiro.au
On 17 August 2014 08:36, Cameron Simpson cs@zip.com.au wrote:
On 16Aug2014 14:44, Mike Wright mike.wright@mailinator.com wrote:
I'm trying to write a simple script that if provided an argument, uses that, or if nothing is provided, uses a predefined string.
This is general shell stuff, not bash specific.
if [ -n $# ]
This is always true. Even "0" is a nonempty string.
Test [ $# -gt 0 ] instead.
Though it's a matter of preference I'd prefer [ $# = 0 ] or [ $# != 0 ] tests unless you really need one of the numeric comparisons. The reason (and it doesn't actually apply for $#, hence matter of preference) is that if you have somehow ended up with a string rather than a number (e.g. an error from wc or something) then you have to think about what your comparison is going to do for that case, whereas the =! and == comparisons will do what you expect. If you do need -gt, -lt and the rest then (except for $#) it's best to check what you've got is a number first.