Exercise
Our First Bash Script Exercise
Task
Write a script, make it runnable, and get it to the point where you can run it by name from anywhere.
- Host
servera- Script
-
/home/student/scripts/take_inventory.sh
-
Write the script in your home directory
Line 1 is the hashbang. It names the interpreter that will run the rest of the file.
cd ~ vim take_inventory.sh#!/bin/bash echo "Taking inventory" ls > inventorycat take_inventory.sh -
Try to run it and fix the permission
A new file is not executable, so the kernel will not run it.
ls -l take_inventory.sh ./take_inventory.shAdd the execute bit and look at the change.
chmod a+x take_inventory.sh ls -l take_inventory.sh -
Run it with ./
./means run the file at this path../take_inventory.sh cat inventory -
Find out why the bare name does not work
take_inventory.shBash searches
PATH, and your home directory is not on it. Look at what is.echo $PATH -
Move it onto your PATH and run it by name
mv take_inventory.sh ~/scripts/ take_inventory.shNo
./this time. Bash found it onPATH, exactly the way it findsls.Clean up
rm -f ~/inventory