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 07-06-2012, 10:36 AM   #1
manyrootsofallevil
Member
 
Registered: Dec 2010
Distribution: Red Hat, Kubuntu
Posts: 130

Rep: Reputation: 14
Calling a Shell script from inside a Shell script


I got roped in to help a colleague at work and I'm a total bash newbie. I'm trying to invoke a script from inside another script

Here are the scripts:

test.env

Code:
echo Works!
delete_correspondence.sh

Code:
#!/bin/sh 
sh test.env
..
echo "Batch Process finished at `date`"
The problem is that test.env doesn't run from inside delete_correspondence.sh

Code:
sh -x delete_correspondence.sh
+ sh $'test.env\r'
: No such file or directory
I've also tried with the full path to test.env but to no avail.

This is not a homework question, test.env will set up the environment variables but my colleague doesn't have that information yet, not sure why.

Oddly enough, for me anyway, this works. In other words, it displays Works! in a loop
Code:
echo Works!
sh test.env
Any ideas?

TIA
 
Old 07-06-2012, 11:36 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Don't run "sh test.env" - just make test.env executable and run "test.env".
 
Old 07-06-2012, 03:24 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by manyrootsofallevil View Post
Code:
sh -x delete_correspondence.sh
+ sh $'test.env\r'
: No such file or directory
\r indicates a carriage return; windows/dos uses \r\n to indicate end of line, but unix uses only \n. You can convert to unix line endings with unix2dos (aka u2d), or with any decent text editor.
 
Old 07-07-2012, 09:10 AM   #4
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
As the purpose is to set environment variables, it’s necessary to source the test.env instead of executing it. Otherwise they are set in subshell only and vanish on return. Either of:
Code:
source test.env
. test.env
 
Old 07-09-2012, 02:47 AM   #5
manyrootsofallevil
Member
 
Registered: Dec 2010
Distribution: Red Hat, Kubuntu
Posts: 130

Original Poster
Rep: Reputation: 14
Removed the shebang and it works fine now. I guess it was getting confused between default shell (BASH) and Bourne shell.

with #!/bin/sh

Code:
sh delete_correspondence.sh
and
Code:
./delete_correspondence.sh
don't work
Code:
bash delete_correspondence.sh
does work

without #!/bin/sh

Code:
sh delete_correspondence.sh
doesn't work
Code:
bash delete_correspondence.sh
and
Code:
./delete_correspondence.sh
do work

Code:
./delete_correspondence.sh
@Reuti

The script was originally running with
Code:
. test.env
, we just changed it to test things out (i.e. in desperation)
 
Old 07-09-2012, 05:49 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
No, shebang or "it" was never confused, the OS (or shell) always knows what to do and how to do. I think you are confused because you do not know what happened.

when you explicitly defines the shell (so executes: <interpeter> <scriptname>) the shebang has no effect - or maybe the flags of the shebang has some influence in some cases. So sh delete_correspondence.sh will use sh and it does not work, bash delete_correspondence.sh works.
When you execute the script itself without defining the interpreter the shebang interpreter will be executed (if exist) instead of the script and that interpreter will process the script. So using #!/bin/sh will execute sh delete_correspondence.sh again which fails. Without shebang the current interpreter/shell will be invoked to process the script. Finally, in your last example (using source or . ): . delete_correspondence.sh the current actual shell will process the script without invoking any other shell therefore the shebang has no meaning in this case again.

The root cause of your problem is probably the \r which was already mentioned, you need to remove it. bash could handle it (silently ignored), but sh assumed this is still part of the filename and could not find the file. Probably you edited this file under windows?
 
1 members found this post helpful.
Old 07-10-2012, 01:56 AM   #7
manyrootsofallevil
Member
 
Registered: Dec 2010
Distribution: Red Hat, Kubuntu
Posts: 130

Original Poster
Rep: Reputation: 14
Quote:
Originally Posted by pan64 View Post
The root cause of your problem is probably the \r which was already mentioned, you need to remove it. bash could handle it (silently ignored), but sh assumed this is still part of the filename and could not find the file. Probably you edited this file under windows?
It was edited using a windows x-windows client, which must add the \r.

I made the changes on a different box, using Putty, which would explain why it works.

Thanks
 
Old 07-10-2012, 04:42 AM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by manyrootsofallevil View Post
It was edited using a windows x-windows client, which must add the \r.
I assume you mean “using a windows x-windows server” and started a client application on the Linux machine. Which graphical application did you use? I assume that using x-windows would hide the platform dependent behavior.
 
  


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] Help calling a shell script inside a python script WyoChuck Linux - Newbie 4 05-28-2012 09:39 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 06:27 PM
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 06:19 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM

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

All times are GMT -5. The time now is 10:46 AM.

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