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 08-06-2015, 11:00 PM   #1
FireWalkWithMe
LQ Newbie
 
Registered: Aug 2015
Posts: 1

Rep: Reputation: Disabled
Talking (Bourne shell) Why does ". ./myscript" run but ". sh myscript" does not?


Scripting newbie here. I do not understand two different errors I get.

In file myscript I have:
Code:
echo "Hello world\n"
I make it executable and I can run it in a forked sub-shell:
Code:
$ chmod +x myscript
$ ./myscript
OR
$ sh myscript
I can also run it in current shell using . (alias of source)
Code:
$ . ./myscript
BUT when I try to do this...
Code:
$ . sh myscript
I get:
Quote:
bash: .: /bin/sh: cannot execute binary file
That's the first error I don't understand.

My intuition is that since I am running it in the current shell with . (AKA source), I am not allowed to tell it to use another shell. Also, I believe I was able to leave out the #!/bin/sh line because I am in bash shell and it is compatible with Bourne shell. I don't think I can get away with that in a different interpreter.

So I tried another experiment. In file myscript (still executable):
Code:
#!/usr/bin/perl
print "Hello world\n";
I can run it in sub-shell:
Code:
$ perl myscript
AND
$ ./myscript
But if I run it in current shell
Code:
$ . ./myscript
I get the second thing I don't understand:
Quote:
Warning: unknown mime-type for "Hello world\n" -- using "application/octet-stream"
Error: no such file "Hello world\n"
which is also what I get if I run the perl script without #! line:
Code:
$ ./myscript
Quote:
Warning: unknown mime-type for "Hello world\n" -- using "application/octet-stream"
Error: no such file "Hello world\n"
Furthermore, I get the same error as above if I run
Code:
$ . perl myscript
Quote:
bash: .: /usr/bin/perl: cannot execute binary file
I say hey... what's going on? (I say heaeeyyyyeyaahheyayeeee... )

Last edited by FireWalkWithMe; 08-06-2015 at 11:03 PM.
 
Old 08-06-2015, 11:14 PM   #2
genss
Member
 
Registered: Nov 2013
Posts: 739

Rep: Reputation: Disabled
http://ss64.com/bash/source.html
"Read and execute commands from the filename argument in the current shell context."

first argument is the file name
there is no file named "sh"
 
Old 08-07-2015, 08:26 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,479

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Whenever you execute a program, any program, it executes in a new shell, a copy of your log in shell, with a new process identification number (PID). You type the command (and any arguments), a copy of your current shell is created, the program executes and the copy shell dies.

A shell program (a "script" is a shell program) can be executed in your current shell (not a copy) with the dot command. This is a common way to modify your current shell, for example you might wish to alter the content of a shell environment variable and you can do that by using the dot command.

The dot command does not work with anything but a shell program.

Something that seems to have gone out of favor is writing a shell program source (with the .sh extension) then making it executable with the make utility (which copies the file without the .sh and makes it executable).

For example:
Code:
prompt: cat hello.sh
#!/bin/ksh

print "Hello, world."
prompt: make hello
cat hello.sh >hello 
chmod a+x hello
prompt: hello
Hello, world.
prompt: . hello
Hello, world.
The "pound-bang" (#!) is how you specify the shell you wish to use for your shell program. In this case, I specified KornShell; I could have specified BASH or Bourne or whatever that way so that my shell program, written in the grammar and syntax of KornShell will run in that shell environment -- my log in shell might be BASH but my program will be executed by KornShell. The shell(s) are binary executable programs: that's why
Code:
. sh hello
does not work (you're already in a shell).

As time goes on, you'll be writing programs in multiple languages; C, C++, shell, AWK, Perl, what have you. You'll learn that you want to use some sort of source code control where you keep the source separate from the executable. That's particularly important when you develop applications that (most likely) will be a mix of sources and the make utility will be a valuable tool for building applications from a mix of source (such as pretty much everything in Linux is). make will make an executable from a shell source file (or C or C++ or whatever) and it will not tack an extension on the executable -- Linux doesn't need a .exe or .bin or .sh tacked on, that's a Windows thing.

Hope this helps some.

Last edited by tronayne; 08-07-2015 at 08:33 AM.
 
1 members found this post helpful.
  


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
Running script on bootup fails, "insserv myscript" ok pingu SUSE / openSUSE 5 05-22-2012 10:42 AM
1st script attempt; cygwin; fails "bash: myscript : command not found" engineerd1 Linux - Newbie 8 11-08-2009 05:03 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
"DigiMemo" and "MyScript" VernerVonTux Linux - Software 1 02-20-2007 07:49 PM


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