Google Search

Google

What is the most suitable distro to use as a server?

Visit Us!

Google Groups
Kuantan Linux User Group
Visit this group

Threat Resource Center | Trend Micro

Sunday, April 18, 2010

Invoke a Script

Use various methods to invoke a script, and determine when it runs in your shell, and when a subshell is created.


1) Using an editor or the cat command, write a korn or bash script that:
* Executes the ps command.
* Echos the value of the shell variable hi.

[root@vmpc_rh804 root]# cat > do-ps.bash
#!/bin/bash
# This script displays active processes
#
ps
echo $hi


2) Set the value of hi to hello in your current command line shell.

[root@vmpc_rh804 root]# hi=hello


3) Execute your script using the shell interpreter.

[root@vmpc_rh804 root]# bash do-ps.bash
PID TTY TIME CMD
1311 pts/4 00:00:00 bash
1358 pts/4 00:00:00 bash
1365 pts/4 00:00:00 ps

** Note that it doesn't recognize the hi variable. This is because the variable is local to the shell in which it was defined. When this script is executed, a new shell is started that is outside your local shell. **


4) Add execute privilege to the script file and run it as a command from the current directory.

[root@vmpc_rh804 root]# chmod +x do-ps.bash
[root@vmpc_rh804 root]# ./do-ps.bash
PID TTY TIME CMD
1311 pts/4 00:00:00 bash
1367 pts/4 00:00:00 do-ps.bash
1374 pts/4 00:00:00 ps


5) Use the built-in source command to execute the commands in the script file.

[root@vmpc_rh804 root]# source do-ps.bash
PID TTY TIME CMD
1311 pts/4 00:00:00 bash
1375 pts/4 00:00:00 ps
hello
[root@vmpc_rh804 root]#


** Note that it is able to recognize the hi variable. This is because the source command runs this script in the local shell. **

No comments:

Linux News Of The Day!

Powered By
widgetmate.com
Sponsored By
Credit Card Forum