Jump to content

Beginners script error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I'm trying to get a script to execute from that's in my text.

#!/bin/sh
echo "Enter a file to search for: \c"
read pname
if [$pname = ""]; then
echo "You must enter a file to search for .."
echo "press any key to continue .."
read press
clear
fi
echo "Searching for $pname .. "
find / -name $pname -print
echo "Press any key to continue .."
read press 

The message that come up when I execute is bash: script.sh: command not found.
I made it executable with command chmod +x script.sh.
I'm not familiar with all the statements in the srcript like fi but can you pinpoint what's wrong with the script?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have you tried "./script.sh"?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Does this script look functional? I don't seem to get a response with command "press any key to continue.." but only by pressing return. Read press means to wait for any key press? If I type in a name, it lists a bunch of files with response permissions denied after each. When it finishes, it says press any key but only pressing return gets me back to a prompt.

#4
Helium

Helium

    Newbie

  • Members
  • Pip
  • 2 posts
read, as stated in the man page, reads a line ( NOT char ) from standard input - end of the line is where you press the return key.

#5
hayschooler

hayschooler

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
I've tried ths again and still no luck getting it to work. The name of the script is findit. I dropped the \c in the first echo since it didn't seem to keep cursor on the same line and also added spaces within brackets, like this [ $pname = "" ].
My script looks like this now.

#!/bin/sh
printf "Enter a file to search for: "
read pname
if [ $pname = "" ]; then
echo "You must enter a file to search for!"
echo "press any key to continue ..."
read press
clear
./findit
fi
 
else
 
echo "Searching for $pname ..."
find / -name "$pname"
echo "Press any key to continue ..."
read press
fi

When I execute this script and don't type anything in and press enter. It works correctly and tells me "You must enter a file to search for!" and clears the screen and starts over again. When I typed a term in to search for, xwindows. I got the following errors.

./findit: line 4: [: xwindows: unary operator expected
./findit: line 12: syntax error near unexpected token 'else'
./findit: line 12: 'else'
./findit: syntax error near unexpected token 'else'
./findit: line 12: else

If anyone can tell me how to arrange the lines or correct the syntax to get this working I'd really appreciate it.

#6
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
The mistake is on using if else condition
It should be
if [] then
....
else
.....
fi
Not
if [] then
.....
fi
else
....
fi

This is what the mistake I see....

#7
Dreamcatcher

Dreamcatcher

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
In linux Os (Especially 10.04 Beta ubuntu edition) it works fine... Totally fine...

#8
konsolebox

konsolebox

    Newbie

  • Members
  • Pip
  • 4 posts
Try to place your variables inside double quotes e.g. "$var"

#9
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
You need to use == instead of =, as in
#!/bin/sh
echo "Enter a file to search for: \c"
read pname
if [$pname == ""]; then
echo "You must enter a file to search for .."
echo "press any key to continue .."
read press
clear
fi
echo "Searching for $pname .. "
find / -name $pname -print
echo "Press any key to continue .."
read press


#10
konsolebox

konsolebox

    Newbie

  • Members
  • Pip
  • 4 posts

DarkLordofthePenguins said:

You need to use == instead of =, as in
There's no need to do that unless you want to compare with patterns.