I have the following script that just archives and clears some log files.
#!/bin/bash
# script: archive_logs_and_clear
# add date to logfile names and copy archive directory
# clear logs
# change to script directory
cd /Volumes/Archive_Volume/00_i7-8600k_z370_backup/rsync_log/
# get list of log files
log_flie_list=$(ls *log.txt)
# get date to add to file names
current_date=$(date "+%Y-%m-%d_%H.%M")
# process log files
for file in $log_flie_list; do
# create filename with date
temp_name=$current_date'_'$file
# copy to archive directory
cp $file ./log_archive/$temp_name
# remove original log file
rm -f $file
# recreate empty log file
touch $file
done
# quit and close the terminal
exit
The issue is the text in red text where I have to hard code a cd to change pwd to the directory where the script file is located.This file is located on the backup drive in the backup directory structure where the log files are written. When I start the script by a double click, pwd defaults to the user home directory and not to where the script is located. I have to make my way over to the script directory or my ls doesn't find anything,etc. This is not a major issue for this case, though I believe it to be in-elegant to have to hard code the script location.
There are other cases where I may have this script on an external drive where it would not be so easy to know the script location ahead of time to code the path. Is there a way to tell make pwd be the directory where the script file is located, or at least automate the location of the script and the cd path?
It should be stable as long as you double-click on a link to invoke your script (as long as you don't change the way you create the link, change any configuration variables that control how links are set, or change the attributes of the link). It should also work if you double-click on the script's name in finder . And, it should also work if you invoke it by giving its name with any absolute or relative pathname to your shell to invoke it, e.g., $PWD/script_name or ./script_name .
If, however, you invoke the script by just using its name (without specifying the directory in which it is located), you could end up with $0 just being script_name if one of the directories included in your $PATH variable is . . If this happens, you can't cd to ${0%/*} . But the following is still likely to work in all cases I can think of with what you're doing: