This is the start of a serialized article
It was started to explain certain features of the bash shell scripting language. I will be posting it across several weeks as time permits. please feel free to ask questions and we can answer them. When the entire article has been serialized, I will send it for free to people who have offered comments.
(Another) simple bash script
If you just type a series of commands into a file and make the file executable, the Linux bash shell will try to run the script as if it is a script written for a bash shell since that is the shell you are probably using if you are running Ubuntu, or many other Linuxes. Scripts for the c-shell or the z-shell might well fail under those conditions, so, to make sure the correct shell is invoked, the first line of the shell-script file should always tell the system what shell is to be invoked. Below is illustrated a simple Bash script: it just outputs the message “hello Linux User!”:
#!/bin/sh # hello.sh this is the name of the file, and is just a reminder # for you what you called it. echo "hello Linux User!" # This is a comment line, anything that follows a '#' sign # is a comment. Comments make a script more readable
Use a text editor, such as vi, nano (for the terminal), gedit or kate (for the GUI), to create a file with the lines above in it. Personally, I recommend nano and kate in that order. Save the file, calling it hello.sh. Then make the hello.sh file executable by typing:
$ sudo chmod 755 hello.sh
This uses the “octal permissions” feature of chmod. It makes the file executable for the owner (probably you, or root), the group and for everybody else. You can then run the script by simply typing ./hello.sh This tells Linux that the file is a script, it is in the directory from which you are calling it, and runs the Linux commands inside it (in this case, just the echo command). [Note: if you are hosting multiple users, you will have to put the files somewhere anybody can get at them, such as /usr/local/bin, in which case you would want to make sure they are owned by root. Then anybody with a sign-in can run the file just by typing hello.sh ]
You can give your shell scripts almost any name you like. There are times when there might be another script built in to your distribution with the same name. Here is the content of the /bin directory of this Ubuntu 9.10 Linux box. Please note that your machine will put in the [name]@[computername]:[directory][access-level] sequence for your prompt as mine put in [wolf]@[Lab5-ubustudio]:[~][$]. If your access-level is shown as [#] then you are acting as root, or the administrative user for that sequence. If you are in the habit of starting sessions as root, this is a marvelous time to break that dangerous habit. In Ubuntu or Debian Linux distributions, you rarely see the root symbol as they use the sudo command to act as pseudo-root for just as long as you need to. Acting as root, when it isn’t required, should be avoided as a general rule.
| wolf@Lab5-ubustudio:~$ ls /bin | ||||
| bash | dbus-daemon | kill | netstat | tar |
| bunzip2 | dbus-uuidgen | less | open | tempfile |
| bzcat | dd | lessecho | openvt | touch |
| bzcmp | df | lessfile | pidof | TRUE |
| bzdiff | dir | lesskey | ping | ulockmgr_server |
| bzegrep | dmesg | lesspipe | ping6 | umount |
| bzexe | dnsdomainname | ln | ps | uname |
| bzfgrep | dumpkeys | loadkeys | pwd | uncompress |
| bzgrep | echo | login | rbash | unicode_start |
| bzip2 | ed | ls | readlink | vdir |
| bzip2recover | egrep | lsmod | rm | which |
| bzless | FALSE | mkdir | rmdir | zcat |
| bzmore | fgconsole | mknod | run-parts | zcmp |
| cat | fgrep | mktemp | sed | zdiff |
| chgrp | fuser | more | setfont | zegrep |
| chmod | fusermount | mount | setupcon | zfgrep |
| chown | grep | mountpoint | sh | zforce |
| chvt | gunzip | mt | sh.distrib | zgrep |
| cp | gzexe | mt-gnu | sleep | zless |
| cpio | gzip | mv | stty | zmore |
| dash | hostname | nc | su | |
| date | ip | nc.traditional | sync | |
| dbus-cleanup-sockets | kbd_mode | netcat | tailf | |
If you just can’t avoid using one of these as a filename for your script, add the .sh and keep it in your home directory in a special file, or on your thumb-drive, and invoke your scripts from their own directory with the ./ sequence. You can check whether your name exists by using the which command. Which tells you where a specific command exists, if it does, and doesn’t tell you that it doesn’t exist, if it doesn’t. For instance to see if there are commands called open and sesame, type:
wolf@Lab5-ubustudio:~$ which open /bin/open wolf@Lab5-ubustudio:~$ which sesame wolf@Lab5-ubustudio:~$
Note the lack of response when you ask for something that doesn’t exist.
This is the end of episode 1. Please stay tuned for another riveting episode soon.
== Wolf Halton is CEO of Atlanta Cloud Technology, Inc. , a guest writer on security topics here, at and owner of Wolfhalton.info.
ty wolf.
i am glad to have you on-board.