Archive for the ‘9 - Scripting’ Category

IX – Scripting

onsdag, juni 23rd, 2010

In a cold, dark world the shell is your only friend along with Jack Daniels. From the shell your able to set environmental variables, run commands and execute binaries. A convenient way to run a set of cmd:s is to pack them into a script file. In some scripts first thing you’ll notice is the so called ’shebang’ on the first script line. It probably looks something like ‘#!/bin/bash‘. In many scripting languages the ‘#’ char indicates a comment which will be disregarded by the script interpreter. And fo sho, this line will be disregarded by the interpreter but instead tells the operating system what script interpreter to use.

======== <helloworld.sh> ========

#!/bin/bash
 
# An array
hello_holder=(Hello bright world)
 
# For each position in the array
for word in ${hello_holder[@]}
do
  # ‘-n’ -> do not output the trailing newline
  echo -n ”$word ”
done
 
echo
echo ”Goodbye cruel world”

======== </helloworld.sh> ========

With the shebang ‘#!/bin/bash’ above we tell the operating system to use ‘bash’ to interpret helloworld.sh. So if we initiate this script in the regular way ‘./helloworld‘ then the shebang will be read by the operating system, specifying bash as interpreter and then ‘bash’ will be invoked with the script as a parameter.
If you’re high on drugs you might recognize the ‘#’ chars as indicating comments. If you’re not on high drugs you might have noticed that as well. With this in mind we now start the script NOT using the regular initiation. Therefore the operating system won’t find the shebang but instead we specify the interpreter to use directly – e.g. ‘bash helloworld.sh‘ or ‘sh helloworld.sh‘. In this case we tell the operating system that we don’t want to execute ‘helloworld.sh’ but instead ‘bash’ with the script as a parameter. And when bash reads the file the shebang ‘#!/bin/bash’ will be disregarded as a comment. Nice.

One thing that might get you fucked up is the permissions here. If you don’t have exec rights to ‘helloworld.sh’ you are unable to initiate the script using the ‘./helloworld.sh’ notation which makes sense. But you are able to run the script with ‘bash helloworld.sh’. How come? Some damn newbie checked in a fix without code review? Naah, not this time. You see, even though you don’t have exec rights to file ‘helloworld.sh’ you have exec rights to initiate ‘bash’ and the script is seen as a parameter to bash. In order to prohibit users from executing this file you must not only take a way their exec rights (to prevent initiations like ‘./helloworld.sh’) but also their read rights (to prevent execution with e.g. ‘bash helloworld.sh’). If a user have no read rights then bash is not allowed to read this file and the script therefore can’t be executed.

One of the most important differences between the different shell is the support for arrays. Bash supports arrays. Sh doesn’t – you are able to run ‘bash helloworld.sh’ but not ’sh helloworld.sh’. I do not know about the other shells but my guess is that they don’t support arrays either.