LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2009, 02:25 PM   #1
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Problem with compiling a shell script


Hi
I am very new to shell scripting and I am trying some handson.I have the following script and I am trying to compile the below shell script


Quote:
#!/bin/sh
# inpath - Verifies that a specified program is either valid as is,
# or that it can be found in the PATH directory list.

in_path()
{
# Given a command and the PATH, try to find the command. Returns
# 0 if found and executable, 1 if not. Note that this temporarily modifies
# the IFS (input field separator) but restores it upon completion.

cmd=$1 path=$2 retval=1
oldIFS=$IFS IFS=":"

for directory in $path
do
if [ -x $directory/$cmd ] ; then
retval=0 # if we're here, we found $cmd in $directory
fi
done
IFS=$oldIFS
return $retval
}

checkForCmdInPath()
{
var=$1

# The variable slicing notation in the following conditional
# needs some explanation: ${var#expr} returns everything after
# the match for 'expr' in the variable value (if any), and
# ${var%expr} returns everything that doesn't match (in this
# case, just the very first character. You can also do this in
# Bash with ${var:0:1}, and you could use cut too: cut -c1.

if [ "$var" != "" ] ; then
if [ "${var%${var#?}}" = "/" ] ; then
if [ ! -x $var ] ; then
return 1
fi
elif ! in_path $var $PATH ; then
return 2
fi
fi
}
but this gives me the following error
./prg_in_path.sh: line 48: syntax error: unexpected end of file
Can anyone help me out?


Ajit
 
Old 09-16-2009, 03:14 PM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by Ajit Gunge View Post
Hi
I am very new to shell scripting and I am trying some handson.I have the following script and I am trying to compile the below shell script




but this gives me the following error
./prg_in_path.sh: line 48: syntax error: unexpected end of file
Can anyone help me out?


Ajit
looks like a missing fi statement.
is this the full script??
i couldnt see 48 lines. am i missing something??
run the script in debug mode.

sh -x scriptname.


paste output here
 
Old 09-16-2009, 03:14 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
The error mesage means that something started on line 48 (like a do-done, a { }, a ( ) or a if-fi) has not been completed by end of file. Have you posted the whole script? It's only 44 lines long, so there is no line 48!

All the same, I tried to scan through it looking for what wasn't closed, much helped by vim's syntax analysis which colour-codes the script. Looked OK in vim so I indented it to better see what is going on but still didn't find what is wrong. Here it is with indentation
Code:
#!/bin/sh
# inpath - Verifies that a specified program is either valid as is,
# or that it can be found in the PATH directory list.

in_path()
{
    # Given a command and the PATH, try to find the command. Returns
    # 0 if found and executable, 1 if not. Note that this temporarily modifies
    # the IFS (input field separator) but restores it upon completion.

    cmd=$1 path=$2 retval=1
    oldIFS=$IFS IFS=":"

    for directory in $path
    do  
        if [ -x $directory/$cmd ] ; then
            retval=0 # if we're here, we found $cmd in $directory
        fi  
    done
    IFS=$oldIFS
    return $retval
}

checkForCmdInPath()
{
    var=$1

    # The variable slicing notation in the following conditional
    # needs some explanation: ${var#expr} returns everything after
    # the match for 'expr' in the variable value (if any), and
    # ${var%expr} returns everything that doesn't match (in this
    # case, just the very first character. You can also do this in
    # Bash with ${var:0:1}, and you could use cut too: cut -c1.

    if [ "$var" != "" ] ; then
        if [ "${var%${var#?}}" = "/" ] ; then
            if [ ! -x $var ] ; then
                return 1
            fi  
        elif ! in_path $var $PATH ; then
            return 2
        fi  
    fi  
}
EDIT:

Shellscript is run, at which time it is "interpreted" so it is not "compiled".

The script as posted only contains function definitions; it never does anything ... ???

Last edited by catkin; 09-16-2009 at 03:16 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Problem Anirban.Adhikary Linux - General 1 04-30-2007 02:47 AM
shell script problem bondoq Linux - Newbie 4 07-02-2006 06:31 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
shell script problem steltner Programming 0 05-17-2003 03:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration