Whats wrong with this 5 line script!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whats wrong with this 5 line script!
# 1  
Old 05-09-2006
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  
Old 05-09-2006
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  
Old 05-09-2006
Thanks for that very constructive feedback but not what i'm looking for
# 4  
Old 05-09-2006
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  
Old 05-09-2006
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  
Old 05-09-2006
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

# 7  
Old 05-10-2006
Hey,

Thanks for that.

do you know whats wrong with this...

Code:
#!/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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sh Script whats wrong?

Hi there, i have a problem i have created followning sh files some years ago but now it dosen`t work anymore i never used it a long time. Can anyone find the Error? Its always runs the stop() block and trying to Killing the Server also if i try to start or creat a new one. #!/bin/sh stop()... (6 Replies)
Discussion started by: NewCannon
6 Replies

2. OS X (Apple)

Whats wrong with this shell script!!!!!

hi guys can you tell me if anything is wrong with this script, seems reasonable to me but somehow never works. Script redacted for being too explicit (2 Replies)
Discussion started by: Freddo
2 Replies

3. Shell Programming and Scripting

Whats wrong with my script?

I am trying to find a value within a properties file and declare it into a variable. Script below. I want the "memSize" to be the branch from the properties file. Right now it always tells me "Not found" What am I doing wrong? #!/bin/sh memsize =''; memSize=`sed '/^\#/d'... (8 Replies)
Discussion started by: vsekvsek
8 Replies

4. UNIX for Dummies Questions & Answers

whats wrong with this?

can anyone tell me why this code doesn't work how its supposed to, its the hangman game but it doesn't play how its supposed to #!/bin/bash NoAttempts="0" livesgiven="5" LivesRemain=$livesgiven LettersAttempted="" wordfile=words numwords=0 function menu() { clear cat << menu... (1 Reply)
Discussion started by: ferrycorsten73
1 Replies

5. Shell Programming and Scripting

whats wrong with this line using perl

E:\>perl -00ne 'push @a,"$_\0$ARGV\n";END{print reverse split/\0/ for sort @a}' file1-obj_prof.out.txt file2-obj_prof.out.txt' Can't find string terminator "'" anywhere before EOF at -e line 1. (6 Replies)
Discussion started by: richsark
6 Replies

6. Shell Programming and Scripting

Whats wrong with this line?

I have a file $I_FILE that I need to filter and store the 1st and the 9th columns in another file $O_FILE. With this in Perl, system ("awk -F, '{print \$1, \$9}' \$I_FILE | sed '\/^\$\/d' > O_FILE"); I get: 4096045055 The first line in I_FILE is:... (5 Replies)
Discussion started by: looza
5 Replies

7. UNIX for Dummies Questions & Answers

Whats wrong in the script?

if then if then echo "fst argument is $1 " else if then "fst argument is $1" fi fi fi Can anyone tell me. My requirement is tht pass a string .. Check whether it contains "-". If yes then check if it... (1 Reply)
Discussion started by: nehagupta2008
1 Replies

8. Shell Programming and Scripting

tell me whats wrong in this?

#! /bin/bash head -5 $1 echo "remove $1 ?" read answer if then echo invalid answer elif rm $1 echo "$1 is deleted" elif then echo file is not deleted else echo "invalid answer" fi What i really want this to do is to ask to delete the file or not..it says something wrong... (1 Reply)
Discussion started by: nadman123
1 Replies

9. UNIX for Advanced & Expert Users

Whats wrong in this Script ???

PATH="/clocal/mqbrkrs/user/mqsiadm/sanjay" MAIL_RECIPIENTS="xyz@abc.com" Subject="File accessed in last minutes:" find $PATH -type f -amin -1 > temp.txt.$$ cat temp.txt.$$ | \ while read line do fuser -uV $line >> tempmail.txt done cat "$tempmail.txt" | mailx -s "$Subject"... (4 Replies)
Discussion started by: varungupta
4 Replies

10. Shell Programming and Scripting

Whats wrong with this script?

Hi all, #!/bin/ksh BIN=/interface/Gunner age=$1 directory="$2" && directory=. cd "$directory" || exit 1 from=`$BIN/today -$age` cd $BIN for i in `cat filestoarchive.txt`;do cd $i find . -mtime 14 | grep -v '.tar$' | $BIN/dttmfilter | awk '$1<="'$from'"{ print;};' | \ done (2 Replies)
Discussion started by: kayarsenal
2 Replies
Login or Register to Ask a Question