sudo apt update && sudo apt upgrade -y |
upgrades your machine with all tools completely |
0 |
passes |
any number other than 0 |
is a fail error |
test -d /tmp/temp_dir test_rc=$? |
check to see if directory was created |
echo "mkdir resulted in ${mkdir_rc}, test resulted in ${test_rc}." |
check return codes |
true && echo "We get here because the first part is true!" true || echo "We never see this because the first part is true :(" |
Check out how an exit status of 0 affects the logical operators: |
false && echo "Since we only continue after && with an exit status of 0, this is never printed." false || echo "Because we only continue after || with a return code that is not 0, we see this!" |
Check out how an exit status of 1 affects the logical operators: |
if [[ $# -ne 3 ]]; then echo "Incorrect usage!" echo "Usage: $0 <directory_name> <file_name> <file_content>" exit 1 fi |
We need exactly three arguments, check how many have been passed to # the script. |
directory_name=$1 file_name=$2 file_content=$3 |
Save the arguments into variables. |
absolute_file_path=${directory_name}/${file_name} |
create absolute path |
if [[ ! -d ${directory_name} ]]; then mkdir ${directory_name} || { echo "Cannot create directory, exiting script!"; exit 1; } fi |
Check if the directory exists; otherwise, try to create it. |
if [[ ! -f ${absolute_file_path} ]]; then touch ${absolute_file_path} || { echo "Cannot create file, exiting script!"; exit 1; } fi |
Try to create the file, if it does not exist. |
echo ${file_content} > ${absolute_file_path} |
File has been created, echo the content to it. |
cp /var/log/dpkg.log dpkg || { echo "Cannot copy dpkg.log to the new directory."; exit 1; } |
Copy the log file to our new directory. |
cd $(dirname $0) |
Change directory to the script location. |
cp /var/log/dpkg.log dpkg || { echo "Cannot copy dpkg.log to the new directory."; exit 1; } |
Copy the log file to our new directory. |
if [[ $# -ne 1 ]]; then echo "Incorrect usage!" echo "Usage: $0 <file or directory path>" exit 1 fi |
input validation |
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets