How do I make a shell script?

How do I make a shell script?

Post by OldUncle » Sun, 06 Sep 1998 04:00:00



Howdy.  I have several questions:

I would like to make an executable shell script that mounts a partition, and one
that unmounts the same.  I have seen directions that start like '# !/bin/sh ...'
but being a totally clueless newbie to the Linux o/s this doesn't help me much!

I would like to command this:  mount -t ext2 /dev/hdb5 /hdb/hdb5 and umount the
same in another script.  From my vantage point it looks like this would be a
good first script, no?

 Which shell would be the one to create it in (ksh, tcsh, csh, ash, zsh?)  If
created in one, can it be run in another?  Do shell scripts need to be compiled?
I feel like I have a handle on Joe editor (embarrassed sigh:  vi is pretty
inscrutable and tho I chose to install emacs, I can't seem to get it to
start....)  

Do I need to set permissions: chmod (####) or aguo-x script-file.name?  How do
you modify the properties of a file from/to binary, executable script, text,
etc?  What are those first letters in the long view:  d, s, etc?

Wouldn't this also be a good entry in fstab to mount mount the partition at
boot--auto, er, magically?  Tho I would like to understand how to create a
script to do this to learn how, I would also like to try it with another
partition so that I can move /usr and maybe /bin to a different drive and mount
this part at boot.  What should the fstab entry look like?
(to mount hdb5 at boot with /usr and /bin??)

Linux is currently installed on a 120 MB drive, hdc1 with hdc5 as a 40 MB swap
extended part.  How can I move /usr and /bin to another drive and set up links
to directories on the hdc1 partition (/usr & /bin) and have the system continue
without a reinstall?

Thanks in advance for advice!  

"Come to think of it, there are already a million monkeys on a million
 typewriters, and Usenet is NOTHING like Shakespeare." Blair Houghton
++++++**********************************************************++++++
******By reading this disclaimer you agree to abide by its terms******
++++++**********************************************************++++++

 
 
 

How do I make a shell script?

Post by Murray Todd William » Sun, 06 Sep 1998 04:00:00


First of all, welcome abord to the Linux world!

By the nature of these questions, it seems you might actually be well served going
to the bookstore and paging through some of the "Linux for Beginners" sorts of
books.  There you'll see enough examples of things being done that you'll start to
clue in to what's going on and how things are done in the Unix world.

I'll touch on a couple of the questions you asked to give you a head start to
figuring everything out.  First of all, you are right in thinking fstab is a good
place to start for getting the appropriate volumes mounted automatically.  It's
probably best to do it that way because usually only the "root" (administrator)
account is authorized to mount volumes.  If you by chance have the RedHat
distribution (can't speak for the others, sorry) you'll also find in X windows a
program called "control-panel" that, when run from root, will help automate a lot
of administrative functions like networking, setting up the mounting tables, etc.

Finally, the trick to #!/bin/sh.  A good thing to pick up on.  UNIX has this really
cool standard with files.  If you have a program (script) file that runs under a
particular language and you include the path to that language's interpreter on the
FIRST LINE of the file, following #!, then you can pretend that file is in itself
an executable and run it from the command line.

Hence, it you wrote a super program in Perl named "super" and had "#!/usr/bin/perl"
on the first line, then you could run the program by simply typing "super" (or
actually "./super" for reasons I wont get into now") at the command prompt
INSTEAD OF "perl super".

Good luck, have fun, and again, welcome aboard!

Murray Todd Williams


> Howdy.  I have several questions:

> I would like to make an executable shell script that mounts a partition, and one
> that unmounts the same.  I have seen directions that start like '# !/bin/sh ...'
> but being a totally clueless newbie to the Linux o/s this doesn't help me much!

> I would like to command this:  mount -t ext2 /dev/hdb5 /hdb/hdb5 and umount the
> same in another script.  From my vantage point it looks like this would be a
> good first script, no?

>  Which shell would be the one to create it in (ksh, tcsh, csh, ash, zsh?)  If
> created in one, can it be run in another?  Do shell scripts need to be compiled?
> I feel like I have a handle on Joe editor (embarrassed sigh:  vi is pretty
> inscrutable and tho I chose to install emacs, I can't seem to get it to
> start....)

> Do I need to set permissions: chmod (####) or aguo-x script-file.name?  How do
> you modify the properties of a file from/to binary, executable script, text,
> etc?  What are those first letters in the long view:  d, s, etc?

> Wouldn't this also be a good entry in fstab to mount mount the partition at
> boot--auto, er, magically?  Tho I would like to understand how to create a
> script to do this to learn how, I would also like to try it with another
> partition so that I can move /usr and maybe /bin to a different drive and mount
> this part at boot.  What should the fstab entry look like?
> (to mount hdb5 at boot with /usr and /bin??)

> Linux is currently installed on a 120 MB drive, hdc1 with hdc5 as a 40 MB swap
> extended part.  How can I move /usr and /bin to another drive and set up links
> to directories on the hdc1 partition (/usr & /bin) and have the system continue
> without a reinstall?

> Thanks in advance for advice!

> "Come to think of it, there are already a million monkeys on a million
>  typewriters, and Usenet is NOTHING like Shakespeare." Blair Houghton
> ++++++**********************************************************++++++
> ******By reading this disclaimer you agree to abide by its terms******
> ++++++**********************************************************++++++


 
 
 

How do I make a shell script?

Post by Steve Holdoway and Julie Holdsto » Sun, 06 Sep 1998 04:00:00




Quote:>Howdy.  I have several questions:

>I would like to make an executable shell script that mounts a partition, and one
>that unmounts the same.  I have seen directions that start like '# !/bin/sh ...'
>but being a totally clueless newbie to the Linux o/s this doesn't help me much!

>I would like to command this:  mount -t ext2 /dev/hdb5 /hdb/hdb5 and umount the
>same in another script.  From my vantage point it looks like this would be a
>good first script, no?

> Which shell would be the one to create it in (ksh, tcsh, csh, ash, zsh?)  If
>created in one, can it be run in another?  Do shell scripts need to be compiled?
>I feel like I have a handle on Joe editor (embarrassed sigh:  vi is pretty
>inscrutable and tho I chose to install emacs, I can't seem to get it to
>start....)  

Try...

vi MountIt      
i#!/bin/sh<CR>
mount -t ext2 /dev/hdb5 /hdb/hdb5<ESC>:wq<CR>

<Subtitles>
vi  gives a blank screen with ~'s on the left side
i enters inseret mode... just keep typing
<ESC> gets you out of insert mode. the : will take the cursor to the
bottom left corner ( and you're now in 'command mode' ), and the  wq
Return writesthe file, then quits vi.
</Subtitles>

you now have a file called MountIt which mounts your disc. Now comes
the bad news. mount requires root priviledges to run. You can make the
shellscript that you've just written mimic this for all users, by
changing the owner of the file to root, and then setting setuid
priviledge on it. ( Login as root, then chown root MountIt, then chmod
4755 MountIt ). However, if this is anything other than your private
plaything then be very aware that this is a potential security
loophole.

Quote:

>Do I need to set permissions: chmod (####) or aguo-x script-file.name?  How do
>you modify the properties of a file from/to binary, executable script, text,
>etc?  What are those first letters in the long view:  d, s, etc?

man chmod should give you quite an insight into the available file
types. Maybe man ls as well.
Quote:>Wouldn't this also be a good entry in fstab to mount mount the partition at
>boot--auto, er, magically?  Tho I would like to understand how to create a
>script to do this to learn how, I would also like to try it with another
>partition so that I can move /usr and maybe /bin to a different drive and mount
>this part at boot.  What should the fstab entry look like?
>(to mount hdb5 at boot with /usr and /bin??)

Yes, it certainly would. The simplest way of getting a new entry in
fstab is to do the following:

        1) manually mount the filesystems that you want.
        2) backup /etc/fstab ( cp /etc/fstab /etc/fstab.old )
        3) mount -p > /etc/fstab

If everything's worked ok, then you should now be able to mount any
partition in fstab just by stating it's name... ie you can now just
type in
          mount  /hdb/hdb5

( Mind you, that's one hell of a mount point, can't yoou mount it as
/newdisc  or similar?)

Quote:

>Linux is currently installed on a 120 MB drive, hdc1 with hdc5 as a 40 MB swap
>extended part.  How can I move /usr and /bin to another drive and set up links
>to directories on the hdc1 partition (/usr & /bin) and have the system continue
>without a reinstall?

I wouldn't do it that way. This is what I'd do.
1) Create two new partitions of the right size to move /usr and /bin
into, an make file systems on them
2) shut down to single user mode
3) make two new directories: /usr.new and /bin.new
4) mount your new partitions on these two directories respectively
5) Copy the files across to the new partitions as follows:
        cd /usr
        find . |  cpio -dumpx /usr.new
        cd /bin
        find  . |  cpio -dumpx /bin.new
6) Create a new /etc/fstab as suggested above, and then edit it,
removing the .new from the lines mounting /usr.new and /bin.new, so
they now mount /usr and /bin
7) rename the directories /usr to /usr.old and /bin to /bin.old ( mv
/usr /usr.old and the same for /bin )
8) Create new directories /usr and /bin ( mkdir... ) ensure that
they're the correct ownership and access modes. Note that mkdir, chown
and chmod are in /bin, so you'll have to type /bin.old/mkdir to
actually find the commands!
9) Reboot the system. Do not do anything else between steps 8 and 9,
as you'll probably find that things will act strangely!

Once the system is up any running to your satisfaction, then you can
get rid of the old copies of /usr and /bin to regain your disc space (
rm -rf /usr.old and so on )

I reckon that when you've completed all of this, then you'll have a
good understanding of how all of this hangs together! It's a bit
bigger than a first project, though!

Quote:

>Thanks in advance for advice!  

>"Come to think of it, there are already a million monkeys on a million
> typewriters, and Usenet is NOTHING like Shakespeare." Blair Houghton
>++++++**********************************************************++++++
>******By reading this disclaimer you agree to abide by its terms******
>++++++**********************************************************++++++

But before you start, read the man pages so you're happy about the
concepts of the above, otherwise things could go terrible wrong!

Good luck,

Steve

 
 
 

How do I make a shell script?

Post by Kevin Mart » Sun, 06 Sep 1998 04:00:00




> Which shell would be the one to create [a new script] in (ksh, tcsh, csh,
> ash, zsh?)  

sh is considered "standard" for this sort of scripting.

Quote:> If created in one, can it be run in another?

That's why those scripts begin with the line such as "#! /bin/sh".
#! is a way of telling Unix to use a certain shell, regardless of the one
you're logged in to.  That way you can be sure that your script is
interpreted the way you intended (e.g. if you're running csh, you don't want
to feed it a sh script by accident, or vice-versa).

Quote:> Do shell scripts need to be compiled?

Nope.  They're interpreted by the shell.

Quote:>Do I need to set permissions: chmod (####) or aguo-x script-file.name?

Yep.  "chmod +x scriptname" will do it.  And it needs to be in your "$PATH"
too.  By default, just being in the same directory as the script WILL NOT
put it on your PATH.  Make a little "bin" directory under your $HOME
directory and put your scripts there.  You can add a line to your .profile
or .bashrc to add it to the end of your PATH at login:

        PATH=$PATH:$HOME/bin

Quote:>  How do
>you modify the properties of a file from/to binary, executable script, text,
>etc?

Whoops.  Scripts *are* text. A binary is a binary, and that aspect of its
nature isn't something you toggle with a command.  You can toggle an
executable flag, yes -- see "man chmod" for that.

Quote:> What are those first letters in the long view:  d, s, etc?

d is for directory.  Try "man ls" for the*details.

Quote:>How can I move /usr and /bin to another drive and set up links
>to directories on the hdc1 partition (/usr & /bin) and have the system continue
>without a reinstall?

I'd take a stab at this, except it's a bit ambitious compared to the
questions that came just before it, and I don't want you saying I hosed your
working system.  Sorry!
 
 
 

How do I make a shell script?

Post by Gary Johns » Mon, 07 Sep 1998 04:00:00



> Howdy.  I have several questions:
>                             Tho I would like to understand how to create a
> script to do this to learn how, I would also like to try it with another
> partition so that I can move /usr and maybe /bin to a different drive and
> mount this part at boot.

Putting /usr on another drive sounds fine, but I wouldn't put /bin on a
different drive from / if I were you.  Many of the fundamental system
administration commands are in /bin, and if something goes wrong, it's
nice if not essential to be able to boot to a minimal system and have
these commands available.  In particular, since mount is in /bin, you
have to have access to /bin in order to mount any other partitions.

Gary

 
 
 

How do I make a shell script?

Post by Michael Carma » Thu, 10 Sep 1998 04:00:00


Okay, I'll try to address at least some of your questions here, but I'm not going to
just tell you how. You did want this to be a learning experience, no?

Quote:> I would like to make an executable shell script that mounts a partition, and one
> that unmounts the same.  I have seen directions that start like '# !/bin/sh ...'
> but being a totally clueless newbie to the Linux o/s this doesn't help me much!

The "#!/bin/sh" as the first line of a file tells the OS which shell to use when
running the script. What commands you can use in the script depend somewhat on which
shell you use. If you're only using things like "mount," which are part of the OS
and not the shell, you shouldn't have to worry about that.

Quote:> I would like to command this:  mount -t ext2 /dev/hdb5 /hdb/hdb5 and umount the
> same in another script.  From my vantage point it looks like this would be a
> good first script, no?

Well, actually I would say that it would be a better first alias if it's something
you want to use often. My reasoning is thus: Alaises are ways to make often used
commands easier to type/remember. Scripts are generally used for longer or more
complex tasks, especially if you need to set some conditional if-then logic. E.g., I
alias 'ls' to 'ls -F' because I like to see flagging for
directories/links/executables. In my .zshrc (which is a script file, just not one
you would normally run manually) I have a loop setup to add things to my PATH, but
only if they aren't in there already.

Quote:> Which shell would be the one to create it in (ksh, tcsh, csh, ash, zsh?)  If
> created in one, can it be run in another?  Do shell scripts need to be compiled?

If all you're doing is invoking OS commands, it really doesn't matter. As I said
above, the first line tells the OS what shell to use when invoking the script, and
this is completely independant of the shell you're running when you call the script.
Shell scripts are true scripts, which means that they are interpreted, not compiled.

Quote:> I feel like I have a handle on Joe editor (embarrassed sigh:  vi is pretty
> inscrutable and tho I chose to install emacs, I can't seem to get it to
> start....)

Heh, I use vi, but only because I didn't have any other choice when starting with
Unix. Learning vi is not for the faint of heart. What's scary is when you actually
start to not only like it, but to *prefer* it for most tasks.

Quote:> Do I need to set permissions: chmod (####) or aguo-x script-file.name?  How do you
> modify the properties of a file from/to binary, executable script, text, etc?
> What are those first letters in the long view:  d, s, etc?

Yes and no. To execute a script named "foo" by typing "foo" you need to make it
executable (and have the directory it's in be in your path). However, for shell
scripts you could also type "source foo" to accomplish the same thing. I won't try
to explain why you might want to do it one way over the other here. A file's
properties don't say anything about it being binary or ascii. They are just
permissions which determine who can do what: read, write, and execute permissions
for each of the user (owner), group, and other (everybody else). The first letters
in the long view tell you what the file is. a '-' means its just a regular file,
while 'd' means that it's a directory, and 'l' means that it's a link. I can't
remember 's' off the top of my head.

Quote:> Wouldn't this also be a good entry in fstab to mount mount the partition at
> boot--auto, er, magically?

Well, for this application, yes. But you're right in thinking that this might be a
nice, small project to start with for trying to learn to script.

Happy coding!

-mjc

 
 
 

1. Accessing a shell script w/o doing . /script

I telneted into a Solaris 2.5.1 server as a regular user and then SU
password: edited my path in my .profile as:
PATH=/usr/bin:/$HOME/bin:/usr/ucb:/etc:. hoping this would enable me
to access files and scripts w/o getting ksh not found from the shell.
Every time I try to execute a script from / dir i have to .
/scriptname. Why is this happening and how can I rectify this problem?


2. PPP-ON not responding

3. handling SIGINT in shell scripts when executing another shell script.

4. Yggdrasil or Slackware...WHERE IS dll ?

5. making a shell script a process group leader

6. Telnet Question

7. Q: How can I have a shell script call another shell script...

8. EXT2-fs warning: bit already cleared for inode XXXXX ?

9. Need a simple shell script made

10. making file in a shell script via cat command.

11. Shell script invoking other shell scripts

12. making shell scripts

13. ? on making menu script a users default shell