LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-21-2011, 08:34 PM   #1
gvanto
Member
 
Registered: Oct 2009
Posts: 40

Rep: Reputation: 0
Shell script returns error when using 'source'


For some reason the script below works fine when I run it directly but when I run is using source, I get a not found error?

With source:
(line 11 is where the 'test' statement is)
Quote:
gtonder@sydlnx03:~> source set_aliases.sh -v 11:31AM
1
set_aliases.sh
-v
/users/gtonder/pwe/scripts/set_aliases.sh:11: = not found
Without source:
Quote:
gtonder@sydlnx03:~> set_aliases.shv -v
1
/users/gtonder/pwe/scripts/set_aliases.sh
-v
...
Code:
#! /bin/sh
# Sets aliases

VERBOSE=false

echo $#
echo $0

if test \( $# -eq 1 \) -a \( "$1" == "-v" \)
then
   VERBOSE=true
fi

if $VERBOSE ; then
    set -x
fi

#makes:
alias mk='make 16'
alias clean_mk='make clean; mk;'

if $VERBOSE ; then
    set +x
fi
 
Old 03-21-2011, 10:54 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Somehow I have the idea that what you posted in not exactly the output on your screen, but that you edited in order to post. This is what I see:
Code:
jlinkels@jlinkels-lt:/tmp$ source set_aliases.sh -v 11:31AM
2
/bin/bash
jlinkels@jlinkels-lt:/tmp$ source set_aliases.sh -v
1
/bin/bash
++ alias 'mk=make 16'
++ alias 'clean_mk=make clean; mk;'
++ true
++ set +x
jlinkels@jlinkels-lt:/tmp$ source set_aliases.sh -v 11:31AM
2
/bin/bash
jlinkels@jlinkels-lt:/tmp$ ./set_aliases.sh -v 11:31AM
2
./set_aliases.sh
jlinkels@jlinkels-lt:/tmp$
In your example, calling set_aliases with '-v 11:31AM' your program says it has received on one parameter.

Check if your system uses Bash for sh.
Code:
jlinkels@jlinkels-lt:/tmp$ which sh
/bin/sh
jlinkels@jlinkels-lt:/tmp$ file /bin/sh
/bin/sh: symbolic link to `bash
'

jlinkels
 
Old 03-21-2011, 11:06 PM   #3
gvanto
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 0
sorry, minus the "11:31AM" ! (that got copied when I copied from the putty console ...

so it should be:
Quote:
gtonder@sydlnx03:~> source set_aliases.sh -v
1
set_aliases.sh
-v
/users/gtonder/pwe/scripts/set_aliases.sh:11: = not found
 
Old 03-22-2011, 06:08 AM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Again, it works here:
Code:
jlinkels@jlinkels-lt:/tmp$ source set_aliases.sh -v
1
/bin/bash
++ alias 'mk=make 16'
++ alias 'clean_mk=make clean; mk;'
++ true
++ set +x
jlinkels@jlinkels-lt:/tmp$
Did you try to find out what /bin/sh points to?

jlinkels
 
Old 03-22-2011, 09:22 PM   #5
mirni
LQ Newbie
 
Registered: Nov 2008
Location: pacific
Distribution: CentOS, openSuse, Ubuntu
Posts: 12

Rep: Reputation: 0
you have 2 equal signs there in the test. To compare strings, bash uses only one.
Code:
if [ "$1" = "-v" ] ; then .... ; fi
 
Old 03-22-2011, 09:24 PM   #6
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
Single and double equal signs are both allowed. And why does the code run correctly on my computer?

jlinkels
 
Old 03-22-2011, 09:52 PM   #7
gvanto
Member
 
Registered: Oct 2009
Posts: 40

Original Poster
Rep: Reputation: 0
mirni, it worked, thanks alot!

Perhaps the different shell versions run differently in their parsing of equality operators??

Thanks again guys for the help.

gvanto
 
  


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
[SOLVED] cURL POST data command from shell returns HTTP Error Code 408 fyndr Linux - Software 3 03-04-2011 10:17 AM
How to execute a shell script (with source command)???? kebabbert Programming 9 11-21-2010 12:06 PM
[SOLVED] Source or Execute an external Script from shell script helptonewbie Linux - Newbie 2 08-11-2010 01:48 PM
installation of wxpython source returns strange error srinivasmiriyalu Linux - Newbie 8 01-21-2010 03:18 AM
using source inside a shell script DEF. Programming 8 11-18-2009 06:52 PM


All times are GMT -5. The time now is 04:57 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