Linux Classes
Linux Training
* Linux Classes *

LINUX CLASSES - PROGRAMMING

How Do I Execute a Linux Shell Script?

So how do you run this little wonder of technology? In DOS, all you have to do is name a file with a .bat extension and it'll be recognized as an executable file--but not so with Linux. Since Linux attaches no meaning to file extensions, you have to mark the file as executable by using the chmod command, like this:

$ chmod +x deltemp

The x marks the file as executable; if you list the permissions for the deltemp file afterward, you will see the x in position four, confirming this:

$ ls -l deltemp
-rwx------ 1 hermie other 55 Feb 19 14:02 deltemp

If you want other users to be able to run this script, give them both read and execute permission, like so:

$ chmod ugo+rx deltemp
$
ls -l deltemp
-rwxr-xr-x 1 hermie other 55 Feb 19 14:04 deltemp

Now the permissions show that any user can view or execute the deltemp script, but only you can modify it. To run the script, just enter its name at the command prompt, prefixed with ./ as shown here:

$ ./deltemp

Note: If the current directory is in the PATH environment variable, you can omit the ./ before the name.

But there's one important thing you should know about running shell scripts this way. When you enter the shell script name and tell Bash to run it, a subshell is created for the execution of the script. This subshell inherits all the shell environment variables, including the current directory, but you lose any changes made to that environment when the script terminates.

What's the practical meaning of this? Well, you might expect that the current directory would be /tmp after you've run the deltemp script, but it's not. You'll still be in hermie's home directory. And if we had set an environment variable inside the script, its value would be good only during the execution of the script. Here's an example to demonstrate this point. Create an executable setvar script with these lines:

PS1='My Prompt: '
echo $PS1

Now watch how the values of the current directory and the PS1 variable change:

$ PS1='Bash Me! '
$
echo $PS1
Bash Me!
PS1 before setvar.
$
setvar
My Prompt:
PS1 during setvar.
$
echo $PS1
Bash Me!
PS1 after setvar.

It looks like this script is absolutely useless for the intended purpose of setting the environment variable. But a little trick will make it work the way you want. Run the script by prefixing the name with a dot and a space, like so:

. setvar

This tells Bash to run the setvar script in the current shell environment, instead of creating a temporary subshell. Verify for yourself that the command prompt does in fact change to My Prompt: after running this script.

Previous Lesson: Linux Shell Scripts
Next Lesson: Shell Script Variables

[ RETURN TO INDEX ]

Comments (most recent first)

Bob Rankin     (21 Aug 2010, 17:46)
@sandeep - That's not enough information to diagnose your problem. Saying "not behaving like root" is like "sometimes my car doesn't work." What specifically is the problem?
sandeep     (21 Aug 2010, 09:20)
hi,
i am logging in as root ....
but it is not executing all commands that a root can do.it is displaying as "root#" but not behaving like root..........
any suggestion?
thanks...
niit     (20 Aug 2010, 02:44)
thanks for ur tutorial.........
Bob Rankin     (02 Aug 2010, 11:13)
Also, the sudo command might be helpful.
Bob Rankin     (02 Aug 2010, 11:10)
@Rahul - The su command cannot accept a password, but you could use ssh to connect to localhost, and pass the command. The ssh command can store passwords with ssh-keygen, and eliminate the need to key it in.
Rahul     (01 Aug 2010, 12:58)
Hi Bob
can i gave the password in my script automatically for ex.
i want to switch from user rahul to oracle using
su - oracle
through scripting
then how it get the password
us
Kwabena     (04 Jul 2010, 13:30)
Hi Bob,

I want to run executables by just typing the name of the executable file instead of typing ./ first (./name). I want to write a shell for this and place it in the /usr/bin directory. Can you give me the code?

Thanks,
Kwabena
Bob Rankin     (02 Jun 2010, 06:14)
@hari - That's like cutting off the branch you're standing on... you'll both fall to the ground. You might consider saving the task ID of the first task in a file, then launching the second task as a background task (not nested). Then the second task can read the task ID of the first from the file, and issue a 'kill' command. Still, it seems there is probably a cleaner way to do what you want.
hari     (02 Jun 2010, 03:43)
Hi....i've one script nested inside another script...i need to stop the execution of the outer script once an if condition in the inside script is satisfied..how can i stop running this outer script??any command for it???
Bob Rankin     (01 Jun 2010, 10:35)
@Dimitris - That can certainly be done. Most likely your script is not running in the correct directory. Try using absolute addressing for the directory in your script.
Dimitris     (01 Jun 2010, 03:06)
Hi , i am trying to write a script that executes some commands. i want to have my script in a folder an through the script i want to be able to use the cd command to move into the subfolders. can this be done ? because i use the cd and i receive an error the folder does not exist.
Bob Rankin     (14 May 2010, 05:56)
I think if you use su without the -c flag, it opens a new shell and your script is suspended until you return. I don't know what you mean about automating the password.
Albert Sefia     (13 May 2010, 07:35)
Sorry about my questions Bob, can i automate password in a linux script? i.e.
passwd albert
......
.....
Albert Sefia     (13 May 2010, 07:31)
Thanks so much Bob, it worked exactly as you said. It thus, executes the commands as the other user but returns to the original user afterwards. Is there a way I can remain as the user i switch to?
Bob Rankin     (16 Mar 2010, 14:28)
Try this: su - username -c "command1;command2;..."
albert sefia     (16 Mar 2010, 12:29)
i wrote a script in Linux. Included in the script was "su - user".
I noticed the script runs until it switches user and then stops. Refusing to execute the commands after the su command. What might be the the problem?

*Name:
Email:
Notify me about new comments on this page
Hide my email
*Text:
 

Ask Bob Rankin - Free Tech Support
<Send This Link to a Friend>         <Bookmark This Page>


Copyright © by Bob Rankin
All rights reserved - Redistribution is allowed only with permission.