The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Whats wrong in the script? nehagupta2008 UNIX for Dummies Questions & Answers 1 06-02-2008 06:24 AM
tell me whats wrong in this? nadman123 Shell Programming and Scripting 1 04-10-2008 01:11 AM
Whats wrong in this Script ??? varungupta UNIX for Advanced & Expert Users 4 08-31-2007 04:02 AM
Whats wrong with this script? kayarsenal Shell Programming and Scripting 2 08-25-2006 09:58 AM
whats wrong with this awk??? george_ Shell Programming and Scripting 5 04-04-2006 04:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-09-2006
c19h28O2 c19h28O2 is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 34
Whats wrong with this 5 line script!

Hi

Code:
#!/bin/sh

user=$1

if [ "$user | grep [0-9]" -eq 0 ]
        echo "No"
else
        echo "Yes"
fi
I'm not quite sure whats wrong with this but I know its something silly.

Any ideas?

Thanks
  #2 (permalink)  
Old 05-09-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Besides the "if" syntax error, you have a major misunderstanding on what can be inside the test brackets

Code:
user=$1

if [ "$user | grep [0-9]" -eq 0 ]
then
        echo "No"
else
        echo "Yes"
fi
Code:
$ ksh -x test.sh bob
+ user=bob
+ [ bob | grep [0-9] -eq 0 ]
test.sh[3]: bob | grep [0-9]: 0403-009 The specified number is not valid for this command.
+ echo Yes
Yes
  #3 (permalink)  
Old 05-09-2006
c19h28O2 c19h28O2 is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 34
Thanks for that very constructive feedback but not what i'm looking for
  #4 (permalink)  
Old 05-09-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Sorry, I was only prompting you to examine the code (through the trace) since the error is clear. I really don't know what you are trying to acheive with "$user | grep [0-9]". Nothing will be "grep'd" because KSH doesn't know that you are trying to grep anything. KSH thinks that this is one big string. Your test needs to be along the lines of this:

[ $(some shell commands like piped through grep) -eq 0 ]

You are going to run into more problems since "$user" is treated as a command when inserted into $(); I'm certain that you want to echo this through grep.
  #5 (permalink)  
Old 05-09-2006
c19h28O2 c19h28O2 is offline
Registered User
  
 

Join Date: Apr 2006
Posts: 34
Hi,

Apologies, think we got off on the wrong foot, i've just started to learn scripting, it was just to test the word entered in $1 if it started with a digit or not, but i wanted to use an if statement to do it.....

Thanks for your help
  #6 (permalink)  
Old 05-09-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
No worries.

You can test for digits using built in functionality rather than a process.

Code:
if [[ ${user##+([0-9])} != ${user} ]]
then
    echo "$user starts with a digit"
else
    echo "$user does not start with a digit"
fi
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0