- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: test with -n or -z not working
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 07:55 PM
тАО09-07-2006 07:55 PM
"sh: 1: Parameter not set."
The code that is failing is:
if [[ -n ${1} ]] ;
then
...
else
...
fi
I've replaced the if statement with the following to get it to work:
if [ $# -eq 1 ] ;
then ...
Can someone explain what's going on?
Thanks,
Steve
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 08:24 PM
тАО09-07-2006 08:24 PM
Re: test with -n or -z not working
have you tried:
#!/usr/bin/sh
echo $1
if [[ -n ${1} ]]
then
echo string found
else
echo emptry string
fi
Ensure spaces between -n and bracket and } and bracket.
It will test for the presence of a parameter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 08:26 PM
тАО09-07-2006 08:26 PM
Re: test with -n or -z not working
So you need to use the following instead:
if [[ -n ${1-} ]]; then ...
The "-" says to set it to null if it is not set.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 08:31 PM
тАО09-07-2006 08:31 PM
Re: test with -n or -z not working
the code tested with double brackets worked.
> cat test-2B.ksh
if [[ -n ${1} ]]
then
echo true
else
echo false
fi
> sh test-2B.ksh xxx
true
> sh test-2B.ksh
false
however if you use only one bracket, it will not.
> cat test.ksh
if [ -n ${1} ]
then
echo vrai
else
echo faux
fi
> sh test.ksh xxx
vrai
> sh test.ksh
test.ksh: test: Specify a parameter with this command.
faux
>
it is quite simple to understand, when ${1} resolve to nothing ($1 is not set)
the test is
if [ -n ]
and -n argument of test, expect a string.
solution is to quote the string : "${1}"
I can't see why it work on AIX/SUN, which shell are you using ? ksh ? bash ? csh ?
Jean-Yves Picard
- Tags:
- quoting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 11:43 PM
тАО09-07-2006 11:43 PM
Re: test with -n or -z not working
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 11:56 PM
тАО09-07-2006 11:56 PM
Re: test with -n or -z not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-08-2006 01:41 PM
тАО09-08-2006 01:41 PM
Solution(I thought I said that. ;-)
>What are the implications of taking that out?
Basically you don't have checking for poorly written shell scripts. As I mentioned, you can replace ${1} by ${1-} and you won't get that error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-09-2006 12:25 PM
тАО09-09-2006 12:25 PM
Re: test with -n or -z not working
TMP=tmp/some_dir
rm -rf /$TEMP
Because of the spelling error, $TEMP was used in the rm command and with set +u in force, the rm command looks like this:
rm -rf /
Having made a similar (but less disastrous) spelling error that deleted files and directories on more than 200 computers, using set -u is mandatory in all my scripts. Putting it in /etc/profile or .profile is also a good idea.
One of the classic shell tasks is to be able to test 3 separate conditions:
1. not defined at all
2. defined with a null value
3. defined with a value
One technique is to assign a special value when the variable is not defined:
VAL=${VAL:-IamNOTdefined}
Now, VAL is either undefined and can be tested with "$VAL" = "IamNOTdefined" or null and can be tested with "$VAL". You can even retain the unset value later in the script like this:
VAL=${VAL:-IamNOTdefined}
... code ...
[ "$VAL" = "IamNOTdefined" ] && unset VAL
Bill Hassell, sysadmin