Re: Shell Scripts (286 Views)
Reply
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 1 of 20 (286 Views)
Accepted Solution

Shell Scripts

Gurus,

I was thinking to learn Shell Scripting now.

I have found this wonderful document in the forum:-

http://forums12.itrc.hp.com/service/forums/questionanswer.do?admit=109447627+1223574328053+28353475&...

How to run a shell script...
Is it like ftp these scripts to the server and do
#sh <scriptname>.sh

or copy and paste the content to the root prompt...

I have done some C programming in school.. but i'm not sure if that would make my job easire..

And If I want to run some simple scripts at the root prompt.. how can I do that..

Thank you
Please use plain text.
Valued Contributor
Adam W.
Posts: 279
Registered: ‎08-06-2008
Message 2 of 20 (286 Views)

Re: Shell Scripts

you can run them as sh .sh, or you can make them executable so that you only have to use .sh

The one thing I ALWAYS forget is the "shabang" at the beginning on the shell script, the #!/usr/bin/sh or whatever. Do you have a particular goal in mind for your scripts or just trying to learn?
There are two types of people in the world, Marines and those who wish they were.
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 3 of 20 (286 Views)

Re: Shell Scripts

First I need to learn and them Implement..

Initially I want to start of with simple scripts like... to create a VG00 or VG01 ... instead of typing the commands everytime... I want to keep a script handy so that whenever needed I will do #sh <scriptname>.sh and the VG is created.

I dont know what else could be an example...

So was just trying to learn... may be in my next project I will have to use...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 20 (286 Views)

Re: Shell Scripts

Hi:

> I was thinking to learn Shell Scripting now...

Very good. *EVERY* good Unix administrator should be able to do basic shell programming at a minimum.

> I have done some C programming in school.. but i'm not sure if that would make my job easire..

Maybe yes; maybe no. Do *NOT* be tempted to extrapolate this into choosing the C-shell! You need to use and learn the POSIX shell --- '/usr/bin/sh' or '/sbin/sh' in our world.

> And If I want to run some simple scripts at the root prompt.. how can I do that..

NO, NO, NO! Work in your own non-root account and learn the pitfalls there, unless, of course, you have good Ignite backups and can afford to rebuild your server after you remove things!

You might start by reading:

http://docs.hp.com/en/B2355-90046/index.html

http://www.shelldorado.com/

Regards!

...JRF...
Please use plain text.
Honored Contributor
Mel Burslan
Posts: 3,213
Registered: ‎08-26-1998
Message 5 of 20 (286 Views)

Re: Shell Scripts

In very simple layman's terms:

(root) # vi /home/user1/myscript

...
write your script here and save & exit
...

(root) # chmod o+rwx /home/user1/myscript

(root) # /home/user1/myscript

this should be all

________________________________
UNIX because I majored in cryptology...
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 6 of 20 (286 Views)

Re: Shell Scripts

take a look at
http://www.shelldorado.com/

lots of tips / best practices / articles

while learning, i would be logged in as root, as a mistake can trash the system. much better to set up a login for a normal user and use that.....
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 7 of 20 (286 Views)

Re: Shell Scripts

$ exit
/usr/bin/rsh: /home/vamsi/.logout: The operation is not allowed in a restricted
shell.
$


Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,953
Registered: ‎03-06-2006
Message 8 of 20 (286 Views)

Re: Shell Scripts

>Mel: chmod o+rwx /home/user1/myscript

Why would you want to do that (add w)?
chmod a+rx /home/user1/myscript
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 9 of 20 (286 Views)

Re: Shell Scripts

"$ exit
/usr/bin/rsh: /home/vamsi/.logout: The operation is not allowed in a restricted
shell.
$"

So? Why is the account running rsh?

I assume this is the account that you set up for yourself so you could play with scripts, is it not? (as usual, no information)
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 10 of 20 (286 Views)

Re: Shell Scripts

When I do the following I have an error.. what does it mean

$ exit
/usr/bin/rsh: /home/vamsi/.logout: The operation is not allowed in a restricted
shell.
$

Also what does the below mean

$ /usr/local/bin/sudo su-
sudo: /usr/local/etc/sudoers is mode 0666, should be 0440
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 11 of 20 (286 Views)

Re: Shell Scripts

yes old school... you are correct..

I have setup this user for myself.. so that I can play with scripts
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 12 of 20 (286 Views)

Re: Shell Scripts

$ chsh testsh /usr/bin/csh
$ echo $SHELL
/usr/bin/ksh
$ chsh testsh /usr/bin/sh
$ echo $SHELL
/usr/bin/ksh
$ chsh testsh /usr/bin/rsh
$ echo $SHELL
/usr/bin/ksh


what ever I change the shell to but when I give the command to see the shell I always see ksh only... Am I giving the incorrect command
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 20 (286 Views)

Re: Shell Scripts

Hi:

I would suggest you look at the 'rsh' manpages:

http://docs.hp.com/en/B3921-60631/sh-posix.1.html

In particular, see the section "rsh Restrictions".

and for:

> $ /usr/local/bin/sudo su-
sudo: /usr/local/etc/sudoers is mode 0666, should be 0440

...this is telling you that the permissions of '/usr/local/etc/sudoers' allows *anyone* to write to the file --- a hugh security hole! Only the owner and the group should have read permission (0440).

Regards!

...JRF...


Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 14 of 20 (286 Views)

Re: Shell Scripts

Hi (again):

I might suggest that instead of first setting up a restricted shell for yourself, use a standard one ('/usr/bin/sh') for your account.

I also strongly urge you to reference the manpages every chance you get. There is a wealth of knowledge therein.

Regards!

...JRF...
Please use plain text.
Honored Contributor
OldSchool
Posts: 3,372
Registered: ‎09-09-2004
Message 15 of 20 (286 Views)

Re: Shell Scripts

"When I do the following I have an error.. what does it mean

$ exit
/usr/bin/rsh: /home/vamsi/.logout: The operation is not allowed in a restricted
shell.
$"

you have set yourself up in a "restricted" shell, "rsh". man "rsh" for more info.
(see the section on "rsh Restrictions")

"$ chsh testsh /usr/bin/csh
$ echo $SHELL
/usr/bin/ksh
$ chsh testsh /usr/bin/sh
$ echo $SHELL
/usr/bin/ksh
$ chsh testsh /usr/bin/rsh
$ echo $SHELL
/usr/bin/ksh"

Well....that's how you wound up with a restricted shell.... the commands above *don't* change the current shell, only the one you get upon a login. you should see the last field of password entry for testsh changing with each execution of the command noted.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,953
Registered: ‎03-06-2006
Message 16 of 20 (286 Views)

Re: Shell Scripts

$ exit
/usr/bin/rsh: /home/vamsi/.logout: The operation is not allowed in a restricted

As mentioned, why use the restricted shell?
Also, do you have a trap command on exit, "trap 0" or "trap EXIT"? I suppose you could have an alias?

$ chsh testsh /usr/bin/csh
$ echo $SHELL
/usr/bin/ksh
>but when I give the command to see the shell I always see ksh only.

Why would you expect this to work, as OldSchool says?
As documented $SHELL is only set by login(1). If you actually invoke another shell, it still won't work:
$ csh # scummy C shell
% echo $SHELL
/bin/ksh
% exit
% $
$ sh # posix shell
$ echo $SHELL
/bin/ksh
Please use plain text.
Trusted Contributor
Nitin Kumar Gupta
Posts: 303
Registered: ‎06-27-2006
Message 17 of 20 (286 Views)

Re: Shell Scripts

Hi,

I am giving some best practices I ever using.

- Create you shell script using vi

- Write first line the shell

#!
#exec 1>/tmp/`basename $0.log 2>&1; date; set -x
Write your code here
exit 0 #very important for successful termination

- Save and exit from the code

- Make the file chmod o+rwx, others to be executable.

- Do not use root user to play with scripts.

IMP: exec 1>/tmp/`basename $0`.log 2>&1; date; set -x
This line will create a log file /tmp/scriptname.log if you uncomment the line. Also you can write set -x to enable o/p trace and set +x to disable o/p trace.

For any further assistance feel free to ask.

Rgds
-NKG-
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,953
Registered: ‎03-06-2006
Message 18 of 20 (286 Views)

Re: Shell Scripts

>Nitin: Make the file chmod o+rwx, others to be executable.

As I mentioned above, you do NOT want to do this. It lets Others write to your script. Perhaps you meant "chmod u+rwx" for User (owner)?

>exec 1>/tmp/`basename $0`.log 2>&1; ...

You should replace `` by $().
And for basename you could use only the shell:
/tmp/${0##*/}.log
Please use plain text.
Trusted Contributor
Nitin Kumar Gupta
Posts: 303
Registered: ‎06-27-2006
Message 19 of 20 (286 Views)

Re: Shell Scripts

Yes,

The mode must be

644 For readable files
755 for executables.

i.e.
chmod 644
chmod 755

-NKG-
Please use plain text.
Trusted Contributor
Grayh
Posts: 1,076
Registered: ‎02-08-2008
Message 20 of 20 (286 Views)

Re: Shell Scripts

thanks everyone
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation