Script Parameters
Explore how to use positional parameters in Bash scripts to make flexible backup scripts. Understand how to pass input paths as parameters, the importance of quoting variables to handle spaces, and how to manage archive filenames to prevent overwriting previous backups. This lesson helps you write scripts that work with any directory, improving script reusability and reliability.
We'll cover the following...
We'll cover the following...
Parametrizing the photo directory
Let’s suppose we moved our photos from the ∼/photo directory to ∼/Documents/Photo. If we want to support the new path in the backup script, we should change its code. The following shows how the new script looks:
#!/bin/bash
tar -cjf ~/photo.tar.bz2 ~/Documents/Photo &&
echo "tar - OK" > results.txt ||
{ echo "tar - FAILS" > results.txt ; exit 1 ; }
cp -f ~/photo.tar.bz2 ~/backup &&
echo "cp - ...