Executing shell script files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing shell script files
# 1  
Old 07-26-2010
Network Executing shell script files

Whats the difference between executing a file such as test.sh as:
Code:
./test.sh

as apposed to
Code:
sh test.sh

i've noticed that a simple while loop will not execute for the 2nd way of doing it, but will for the first. Also what do you guys all recom
# 2  
Old 07-26-2010
Maybe read the numerous answers to your previous post?
https://www.unix.com/shell-programmin...problem-2.html

The theme throughout that was to find out what Shell you were running which could explain the anomolous behaviour and to actually see the script you were running, how you ran it, and any output messages.

We are unwilling to embark on another guessing game. I don't even think we managed to find out what Operating System you had - only that you were using "tcsh".
# 3  
Old 07-26-2010
Quote:
Originally Posted by methyl
Maybe read the numerous answers to your previous post?
https://www.unix.com/shell-programmin...problem-2.html

The theme throughout that was to find out what Shell you were running which could explain the anomolous behaviour and to actually see the script you were running, how you ran it, and any output messages.

We are unwilling to embark on another guessing game. I don't even think we managed to find out what Operating System you had - only that you were using "tcsh".
Methyl,
I apologise and I see where you are coming from. I understand and all I can say is that I am sorry that my knowledge when it comes to unix is not the same as yours, I apologise for ever thinking I could further it by asking questions, I apologise for having the nerve to come onto this forum in the first place, I apologise for not having told you clearly enough in post 9 on that thread that I was using Solaris, but most of all, I apologise for having found out the problem and found the solution, and then having the bloody nerve to ask why it works like that.
I hope with time you can forgive me, and that one day you will realise the world is also full of people who are not as brilliant as you. Next time please try not to put someone down, as thats how I feel. I have just got off the phone from my mother, adn she would like ot apologise for ever giving birth to me.
Regards
# 4  
Old 07-26-2010
No offence linuxkid ... this is just about stating the problem clearly.

1) What is in test.sh ?

2) Example 1
Which shell is running at the time the command is issued?
Is it "tcsh"?
What does "ps" output?
What happened when the script ran?

3) Example 2
Which shell is started by running "sh" ?
A poster on your previous thread though that it was Bourne Shell. If so, this is a very old Solaris. Which version of Solaris?
What does "uname -a" output?
What happened when the script ran?

I was confused by you having "tcsh" because I though that is was the default shell on old Apple MACs and freeBSD. i.e. not Solaris.

The "C" shells (such as csh, tcsh) are quite different from other common Shells such as: Bourne Shell, Korn Shell, bash, Posx Shell etc..
# 5  
Old 07-26-2010
For example, if the first line in test.sh is:

Code:
#! /usr/bin/ksh

Then when you run : ./test.sh , the script will be run in ksh (/usr/bin/ksh).

If you run : sh test.sh ,it will run by /usr/bin/sh (by default).
# 6  
Old 07-26-2010
The syntax for "while" is quite different in "C" shell and Bourne shell. Thus a script can work in one shell and not another.

This script works on "csh" but fails in any Bourne shell. The Bourne versions we suggested in your previous thread all fail in "C" shell with syntax errors in the "while" statement.
Tried with "csh" (I don't have "tcsh" handy).

Code:
#!/bin/csh
# Output the word "Hello" exactly 20 times
set i=0
while ( $i < 20 )
       echo "Hello"
       set i=`expr $i + 1`
end

# 7  
Old 07-26-2010
Hey Linuxkid,

There are many different types of shell implementations, ranging from ksh, to csh, to bash. Most coding these days, I'm told, is done on bash (you'll get more information if you wiki around).

If you run `sh test.sh', then the shell that runs test.sh is the shell that is invoked when sh is called. To find out which shell `sh' invokes, see what it directs to. Your `sh' command will probably sit in `/bin'. If you do `ls -al /bin/sh' (`ls -al' shows all files in the long listing format - see `man ls' for more), you will find that /bin/sh is probably a symlink (you will probably see an arrow like this `/bin/sh -> dash'). For more on symlinks, do `man ln'. A simple description is that symlinks are like shortcuts in Windows. They point to the *actual* command you invoke when you run sh.

Getting back to `sh', the word that follows the arrow, is the command or shell that is actually invoked when you run sh. Thus, for me, when I do `sh test.sh', what happens is that test.sh is run as a script in the *dash* shell.

Now, if test.sh starts with the line `#!/bin/bash', then that is a direction that it should be run using the *bash* shell only! However, this constraint is obeyed only when you run ./test.sh. That is, even if you have `#!/bin/bash', and you run `sh test.sh', test.sh will run in dash (or whatever your `sh' points) regardless of your `#!..' statement (this is sometimes called a shebang or hashbang. See Shebang (Unix) - Wikipedia, the free encyclopedia). The shebang is followed only if you do `chmod +x test.sh' and then run `./test.sh'.

I hope this helps?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why we use -f while executing any shell script?

Hi All, I wanted to know why we use the '-f' option while executing script. in my case... abcd.sh -f any_evts 02 2014 abcd = Scriptname -f = dont know any_evts = Some file or string 02= month 2014 = year So in above pleas ehelp to understand here -f and other arguement like... (1 Reply)
Discussion started by: ajju
1 Replies

2. Shell Programming and Scripting

Executing a shell script using sh

Platform : Solaris 10, RHEL 5.4, OEL 6 I've noticed that some of my colleagues execute scripts by typing sh before the script name sh myscript.shI always execute a script by typing the script name and typing enter provided PATH variable has . (current directory) in it myscript.sh (and... (1 Reply)
Discussion started by: John K
1 Replies

3. Shell Programming and Scripting

Executing a shell script

LD_LIBRARY_PATH=~/tme-0.8/bus/multibus:~/tme-0.8/bus/sbus:~/tme-0.8/dist/softfloat/softfloat/bits32:~/tme-0.8/dist/softfloat/softfloat/bits64:~/tme-0.8/generic:~/tme-0.8/host/bsd:~/tme-0.8/host/gtk:~/tme-0.8/host/posix:~/tme-0.8/ic:~/tme-0.8/ic/ieee754:~/tme-0.8/ic/m68k:~/tme-0.8/ic/sparc:~/tme-0.8/... (1 Reply)
Discussion started by: lucky7456969
1 Replies

4. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

Executing Procedure from shell script..

Hello, I created a sql file to create a Procedure, and it was successfully created. I created a sql file to execute the procedure, and it did without any errors, but i dont see the data been updated. The Execute procedure.sql script is: BEGIN set serveroutput on size 1000000 execute... (5 Replies)
Discussion started by: msrahman
5 Replies

6. Shell Programming and Scripting

diffrence in executing a shell script

hi Could some one let me know what is the diffrence in executing a shell script as below $sh script.sh (this is executed in a subshell) $./script.sh(this is executed in the current shell) $script.sh(this is executed in a subshell) where script.sh is the name of the shell script. ... (1 Reply)
Discussion started by: sowjanya
1 Replies

7. UNIX for Advanced & Expert Users

Executing a shell script from windows;script present in unix

I need to execute a shell script kept in unix machine from windows. User id, password area available. For eg. There's a shell script wich moves all the logs kept in my home directory to a directory named LOGS. Now i need to get this done through windows; either using a batch file, or java... (4 Replies)
Discussion started by: rajneesh_kapoor
4 Replies

8. Shell Programming and Scripting

Executing Multiple .SQL Files from Single Shell Script file

Hi, Please help me out. I have around 700 sql files to execute in a defined order, how can i do it from shell script (3 Replies)
Discussion started by: anushilrai
3 Replies

9. UNIX for Dummies Questions & Answers

Executing a Shell Script

I am trying to run a shell script using the ./<ScriptName> command, but the server returns an error bash: ./Script1.sh: Permission denied What variable do I need to set to avoid this? (4 Replies)
Discussion started by: igandu
4 Replies

10. UNIX for Advanced & Expert Users

Shell script is not executing

Hi, I am trying to execute the below shell script: script name(ss1). ss1 was given permission - 744 before executing. name: ss1 #ss1 #usage:ss1 ls who pwd :wq I tried to execute $ss1 (Enter) Its not executing.... It says that ss1 is not found: echo $SHELL. The o/put i got is... (5 Replies)
Discussion started by: dreams5617
5 Replies
Login or Register to Ask a Question