Closed Thread
Results 1 to 10 of 10

Thread: Beginners script error

  1. #1
    hayschooler is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    31
    Rep Power
    0

    Beginners script error

    I'm trying to get a script to execute from that's in my text.

    Code:
    #!/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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: Beginners script error

    Have you tried "./script.sh"?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    hayschooler is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    31
    Rep Power
    0

    Re: Beginners script error

    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.

  5. #4
    Helium's Avatar
    Helium is offline Newbie
    Join Date
    Mar 2010
    Location
    Riga / Latvia
    Posts
    2
    Rep Power
    0

    Re: Beginners script error

    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.

  6. #5
    hayschooler is offline Learning Programmer
    Join Date
    Feb 2010
    Posts
    31
    Rep Power
    0

    Re: Beginners script error

    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.

    Code:
    #!/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.

  7. #6
    veda87's Avatar
    veda87 is offline Programmer
    Join Date
    Aug 2009
    Posts
    126
    Rep Power
    0

    Re: Beginners script error

    The mistake is on using if else condition
    It should be
    Code:
    if [] then
    ....
    else
    .....
    fi
    Not
    Code:
    if [] then
    .....
    fi
    else
    ....
    fi
    This is what the mistake I see....

  8. #7
    Dreamcatcher's Avatar
    Dreamcatcher is offline Learning Programmer
    Join Date
    Apr 2010
    Location
    Greece
    Posts
    39
    Rep Power
    0

    Re: Beginners script error

    In linux Os (Especially 10.04 Beta ubuntu edition) it works fine... Totally fine...

  9. #8
    konsolebox is offline Newbie
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Re: Beginners script error

    Try to place your variables inside double quotes e.g. "$var"

  10. #9
    DarkLordofthePenguins's Avatar
    DarkLordofthePenguins is offline Programming Expert
    Join Date
    Jul 2010
    Location
    A deep, dark hole in the ground
    Posts
    409
    Blog Entries
    74
    Rep Power
    8

    Re: Beginners script error

    You need to use == instead of =, as in
    Code:
    #!/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

  11. #10
    konsolebox is offline Newbie
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Re: Beginners script error

    Quote Originally Posted by DarkLordofthePenguins View Post
    You need to use == instead of =, as in
    There's no need to do that unless you want to compare with patterns.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Error with drag and drop script
    By MicroBoy in forum JavaScript and CSS
    Replies: 7
    Last Post: 05-26-2011, 07:01 AM
  2. Replies: 2
    Last Post: 07-15-2009, 11:39 AM
  3. php script, no error, but not working right.
    By phpforfun in forum PHP Development
    Replies: 8
    Last Post: 02-13-2009, 01:18 PM
  4. unexpected T_LOGICAL_OR Error in script
    By Skillz in forum PHP Development
    Replies: 1
    Last Post: 12-27-2008, 08:18 PM
  5. Error with my PHP script image() add text thingy
    By edisonlau in forum PHP Development
    Replies: 2
    Last Post: 06-04-2008, 09:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts