Whats wrong with this 5 line script!

Hi

#!/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

Besides the "if" syntax error, you have a major misunderstanding on what can be inside the test brackets

user=$1

if [ "$user | grep [0-9]" -eq 0 ]
then
        echo "No"
else
        echo "Yes"
fi
$ 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

Thanks for that very constructive feedback but not what i'm looking for

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.

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

No worries.

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

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

Hey,

Thanks for that.

do you know whats wrong with this...

#!/bin/sh

hour=$(date | cut -c12-13)
echo $hour

I use ksh and i get syntax error on line 3: 'hour=$' unexpected

Oh also i've tried putting hour=$(date | cut -c12-13) on the command line and it echo's 11 so it is working?

I don't think /bin/sh knows how to use $(foo). Since you are defining your shell as #!/bin/sh, perhaps it's choking on that. Try either defining your shell as #!/bin/ksh (or whatever the path is), or use back ticks (e.g., `foo`) instead.

#!/bin/ksh
hour=$(date | cut -c12-13)
echo $hour

Odd it works fine on my Slackware box under sh and ksh and on OSX under
sh/bash and zsh.

Try
hour=`date | cut -c12-13`
echo ${hour}

Actually that's not odd at all. On all GNU/Linux systems that I have seen, /bin/sh is always a link to /bin/bash. You can check it yourself on your Slackware system. And of course bash understands the $() syntax, so there you have it.

P.S. Take a look at the /bin/sh and /bin/bash files your OSX system too.