Shell Scripting

From Silenceisdefeat

Jump to: navigation, search

How to create and execute a shell script

Login to your account, open a new file in vi. (We're assuming you are in "/home/<yourusername>" if you are using a different folder make sure you adjust the paths below. I use "/home/<yourusername>/scripts"

 vi helloworld.sh

Enter these two lines:

 echo "Hello, World!" 
 echo "Knowledge is power."

Save and exit.

Now do the following:

 -bash-4.0$ bash /home/<yourusernam>/helloworld.sh 
 Hello, World!
 Knowledge is power.

Since "/home" is mounted noexec, you need to feed the script directly to the interpreter, which is also why you do not need the traditional #!/bin/bash or #!/bin/sh in the first line of the script.

noexec results in the following when running scripts the normal way:

 -bash-4.0$ cat helloworld.sh 
 #!/bin/bash
 echo "Hello, World!"
 echo "Knowledge is power."
 
 -bash-4.0$ chmod 700 helloworld.sh 
 -bash-4.0$ ls -al | grep helloworld.sh  
 -rwx------   1 unrest  user       61 Oct 13 11:00 helloworld.sh 
 -bash-4.0$ ./helloworld.sh  
 -bash: ./helloworld.sh: Permission denied

As you can see noexec means "no execution allowed". To check if a directory is mounted noexec, you can do the following:

 -bash-4.0$ mount
 /dev/wd0a on / type ffs (local)
 /dev/wd1a on /home type ffs (local, noexec, nosuid, with quotas)
 /dev/wd1b on /tmp type ffs (local, noexec, nosuid, with quotas)



For more advanced scripting:

http://www.bsdnewsletter.com/bsda-book/Create_a_simple_Bourne_shell_script.html

Personal tools