source (contrary to the popular belief that this has to do with burgers!) is a way of getting commands to run inyour current shell (without opening a new shell, as would normally happen)
. shellscript |
Notice there is a whitespace between the fullstop ( . ) and the shellscript script.
As we have covered before I can run this script with:
./shellscript |
Then the following would happen:
bash (your current shell) would call a script called shellscript
shellscript would start a new shell (as directed by #!/bin/bash inside the script)
the script would run and finally exit.
control would be returned to your original bash shell
If on the other hand we sourced this script using the following command:
. ./shellscript |
The following would happen:
from your current shell, shellscript would be run. Notice, no starting a new shell.
The script would run to completion.
When the exit 0 is reached in shellscript, your current shell would vanish (if you ran this directly after logging in, you would be returned to the login prompt)
Firstly, why does the sourcing not begin a new shell? This is the point of sourcing.
The commands in shellscript are run from the current shell and the shebang at the beginning of the shell has no effect.
Secondly, why would you be returned to your login prompt?
On reaching the exit 0, the current shell exits, as it must. Since you were not running this script within a subshell, the current shell exits, leaving you at the login prompt.
One of the uses I have for sourcing I got from looking at the startup scripts in Linux. In these scripts, they keep a host of variables in files that can be modified by the user. At the time of running the script, they source these files, and bingo, they have variables set according to the users specifications.
I have included an example of this below:
Edit a file called vars and enter the following to set 4 variables:
NAME=Hamish SURNAME=Whittal COMPANY="QED Technologies CC" TELNUM='0828035533' |
You might want to set these variables up-front, before you start your script.
Edit a new script and at the start of your new script include the shebang, as normal, but also include the line '. var' as shown below:
#!/bin/bash . var #sourcing the variables in the file var. echo "$NAME $SURNAME was here" exit 0 |
Now, to get the user to run this with different data in the NAME, SURNAME, etc. fields, they only need modify the 'var' file.
Other uses of sourcing include using it to define a set of reusable functions, then sourcing the functions.sh script inside your shell script:
. functions.sh |
This can help immensely when writing large, complex shell scripts. More examples of using source will be given once we have discussed using functions.
Change your prompt to read as follows:
[ 21 March: 13:12pm : hamish@defender : /home/hamish/shell_scripting ] $ |
.The prompt will naturally not have the exact date specified here, but the date and time will vary according to your system date. The user name will also vary according to who is currently logged in, and the path will vary depending on the path the user is working in at the time.
Modify your eatout.sh script to obtain the default parameters from a file called restaurant.def. This file should contain two variables, namely RATING and TYPE. Ensure that on running the script, if no parameters are supplied, the default ratings are used. Note: you may have to alter your eatout.sh in more than one way, since in the last exercises, eatout.sh without any parameters ran in interactive mode.