LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 07: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, 09:54 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 10: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, 05:08 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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, 08: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, 08:24 PM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

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

jlinkels
 
Old 03-22-2011, 08: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



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 09:17 AM
How to execute a shell script (with source command)???? kebabbert Programming 9 11-21-2010 11:06 AM
[SOLVED] Source or Execute an external Script from shell script helptonewbie Linux - Newbie 2 08-11-2010 12:48 PM
installation of wxpython source returns strange error srinivasmiriyalu Linux - Newbie 8 01-21-2010 02:18 AM
using source inside a shell script DEF. Programming 8 11-18-2009 05:52 PM

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

All times are GMT -5. The time now is 10:34 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
Open Source Consulting | Domain Registration