LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2009, 12:33 PM   #1
leeharris
LQ Newbie
 
Registered: Feb 2009
Posts: 10

Rep: Reputation: 0
Angry Really strange problem - RedHat /bin/sh vs /bin/bash


Hi,

I have a really strange problem that's giving me some major headaches and wondered if the bright minds of linuxquestions might be able to help me out.

This problem is two fold really... I shall explain...

I've got a script in which I need to pass variables from within a loop back to the main script. Using pipes in a loop in bash apparently spawns a subshell and as such the variable values are lost once the loop exits.

So, I had something like this...

Code:
cat file.txt | while read line
do
something
retval=$?
done

echo ${retval}
This didn't work because of the pipe / subshell thing...so I changed it to this...

Code:
while read line
do
something
retval=$?
done < <(cat file.txt)

echo ${retval}
Now this worked fine while I was testing it using the default bash shell on a RedHat EL 4 box.

However, the script is to be used in an automation product (BladeLogic), and when that executes the script it uses /bin/sh to do it instead of /bin/bash and it fails with a message like unexpected < near token ( or some such, basically a syntax error.

Now the really weird thing is, that when I tested the script manually again, still on the same server, I found that if executed it like this...

Code:
/bin/bash myscript
...it worked. But if I did this...

Code:
/bin/sh myscript
...it failed with the syntax error.

But now the really strange part. If on the same server I do which sh it tells me /bin/sh, and if I do ls -l /bin/sh it shows me it that /bin/sh is just a symbolic link to /bin/bash!

And I am completely stumped by this :-(

Thanks - Lee
 
Old 02-12-2009, 01:01 PM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
When called as 'sh', bash behaves differently than if called as 'bash' -it runs more POSIX-ly conformant with only a few extensions. This works for me:

Code:
#/bin/sh
while read line
do
something
retval=$?
done < file.txt

echo ${retval}
 
Old 02-12-2009, 01:35 PM   #3
leeharris
LQ Newbie
 
Registered: Feb 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Hi, yeah I tried the single < but I need to evaluate a command, and doing < (command) didnt work either. But I think I've cracked it... sort of.

As mush as I don't really like this solution, it will have to do for now and that is to stick

Code:
set +o posix
at the top of the script. That seems to work fine now!

Thanks!
 
Old 02-12-2009, 06:05 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,368

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Maybe if you showed us what you're really doing we could come up with a better soln?
 
Old 02-13-2009, 06:35 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
You might also try this:
< $(command)
and especially this:
< `command`
 
  


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
Difference between /bin/bash & /bin/sh paragkalra Programming 11 01-02-2009 06:10 AM
How to add paths to sbin, bin/bash, bin sh for users gopi.d Fedora 3 12-07-2007 12:47 AM
Change /bin/bash to /bin/zsh Smokey Slackware 12 07-14-2004 01:06 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
bin/bash:usr/bin/lpr NO SUCH FILE OR DIRECTORY Adibe_Hamm Linux - Newbie 3 10-14-2003 02:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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