Using Bash Script
Explore how to create a Bash script to automate backup tasks and understand the importance of file permissions and the shebang line. Learn how to run scripts directly, modify permissions with chmod, and ensure the correct interpreter executes your script for consistent behavior on different systems.
We'll cover the following...
We'll cover the following...
Bash script
Let’s create a Bash script that does our backup command. Here are the steps for doing that:
- Run the following command:
vim ~/photo-backup.sh
Bash script
-
Copy the following backup command:
#!/bin/bash (tar -cjf ~/photo.tar.bz2 ~/photo && echo "tar - OK" > results.txt || ! echo "tar - FAILS" > results.txt) && (cp -f ~/photo.tar.bz2 ~/backup && echo "cp - OK" >> results.txt || ! echo "cp - FAILS" >> results.txt) -
Paste the command in the terminal.
-
Press the
esckey on the keyboard and then type:x!to save the file.
To quit without saving we can press the
ecskey on the keyboard and then type:q!.
- View the file created in the home directory with the
photo-backup.shname by:cat ~/photo-backup.sh
Now, we have the Bash script file. We call ...