4

I am learning shell scripting for my introductory UNIX course at my university, and I`m curious if is it possible to have more than one shell script in one text file. What I mean by that is, for example in pascal when you have:

var…

begin
end.
//another program which will be ignore

compiler ignores anything after the end with period. Can something like this be done with Shell? I`m asking because I have to go through lot of Shell examples and having many files opened is not very handy, with this I could run the same script without worrying about u+x permissions and just run the script with same name anytime I go to the next code example. So it is possible to have something that will say: end, do not read any further. ? Thank you in anvance for answering this silly question.

  • 1
    Hello and welcome to ul.sx! If you don't mind typing exit instead of end., then yes, there is. – Bananguin Sep 22 '14 at 12:20
4

For said purpose - that is stopping the script at any given line - you could simply use exit to break at this point. For any real life application this would seem a little crude though but for learning purposes it should fit.

Say:

#!/bin/sh

# now I am working here
do_something
exit

# this is old, do not execute
did_something
exit

# this is even older, do not execute
did_something_long_ago
  • That is exactly what I`m looking for, thank you! – Martin Sep 22 '14 at 12:58
0

You can use the construction : <<"SOMEWORD" ... SOMEWORD which substitute the commenting by #

##### The following "code block" is effectively ignored
: <<"SOMEWORD"
/etc/init.d/mydatabase clean_stop
mydatabase_dump /var/db/db1 /mnt/fsrv0/backups/db1
logger -t SYSHALT "System halt: pre-shutdown actions done, now shutting down the system"
shutdown -h NOW
SOMEWORD
##### The ignored codeblock ends here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.