Database Reference
In-Depth Information
1847 he
1778 of
Note that if you're in the same directory as the executable, you need to execute it as
follows (note the ./ ):
$ cd ~/book/ch04
$ ./top-words-2.sh
If you try to execute a file for which you do not have the correct access permissions,
as with top-words-1.sh , you'll see the following error message:
$ ./top-words-1.sh
bash: ./top-words-1.sh: Permission denied
Step 3: Deine Shebang
Although we can already execute the file on its own, we should add a so-called she‐
bang to the file. The shebang is a special line in the script that instructs the system
which executable should be used to interpret the commands. In our case, we want to
use bash to interpret our commands. Example 4-2 shows what the file top-words-3.sh
looks like with a shebang.
Example 4-2. ~/book/ch04/top-words-3.sh
#!/usr/bin/env bash
curl -s http://www.gutenberg.org/cache/epub/76/pg76.txt |
tr '[:upper:]' '[:lower:]' | grep -oE '\w+' | sort |
uniq -c | sort -nr | head -n 10
The name shebang comes from the first two characters in the line: a hash (she) and
an exclamation mark (bang). It's not a good idea to leave it out, as we have done in
the previous step, because then the behavior of the script is undefined. The Bash shell,
which is the one that we're using, uses the executable /bin/bash by default. Other
shells may have different defaults.
Sometimes you will come across scripts that have a shebang in the
form of !/usr/bin/bash or !/usr/bin/python (in the case of
Python, as we will see in the next section). While this generally
works, if the bash or python (Python Software Foundation, 2014)
executables are installed in a different location than /usr/bin , then
the script does not work anymore. It's better to use the form presen‐
ted here, namely !/usr/bin/env bash and !/usr/bin/env python ,
because the env (Mlynarik & MacKenzie, 2012) command-line tool
is aware where bash and python are installed. In short, using env
makes your scripts more portable.
Search WWH ::




Custom Search