Issue with pwd for script run by double click on script (MacOS High SIerra)

Hello,

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?

Thanks,

LMHmedchem

What be the output of

echo $0, ${0%/*}

in your script?

for echo $0, ${0%/*} I get,

/Volumes/external_backup/00_i7-8600k_z370_backup/rsync_log/archive_logs_and_clear, /Volumes/external_backup/00_i7-8600k_z370_backup/rsync_log

I also tried echo ${BASH_SOURCE[0]} for which I get,

/Volumes/external_backup/00_i7-8600k_z370_backup/rsync_log/archive_logs_and_clear

I guess I would have to parse either output to get the script directory value

 /Volumes/external_backup/00_i7-8600k_z370_backup/rsync_log/ 

I need. Are both of these options stable, meaning that the output format is predictable?

LMHmedchem

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:

#!/bin/bash
[ "$0" != "${0%/*}" ] && cd "${0%/*}"
... ... ...

It might also work with ${BASH_SOURCE[0]} , but that would depend on which version of bash is being used.
Good luck,
Don

1 Like

Here is POSIX shell compliant code, that returns the full path of a file or directory.

#!/bin/sh

# FullPath subroutine, version 1.0, POSIX shell compliant.

# Copyright © 2019 Ioannis Vranos

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.



# Displays the full path, of the file or directory argument.

FullPath()
{

 fdName="$1"


 dirName="$(dirname "$fdName")"

 fdName="$(basename "$fdName")"


 #shellcheck disable=SC2164
 cd "$dirName" 2>/dev/null


 rc=$?

 if [ "$rc" -ne 0 ]
 then

  echo
  echo "Error: Can not access $1"
  echo
  echo "Exiting..."

  exit 1

 fi



 if [ -d "$fdName" ]
 then


  #shellcheck disable=SC2164
  cd "$fdName" 2>/dev/null


  rc=$?

  if [ "$rc" -ne 0 ]
  then

    echo
    echo "Error: Can not access $1"
    echo
    echo "Exiting..."

    exit 1  

  fi


  fullPath="$(pwd)"


  else

  fullPath="$(pwd)"

  fullPath="$fullPath/$fdName"


 fi


 echo "$fullPath"

 return 0
}

Here is the GPLv3 License: GNU General Public License v3.0 - GNU Project - Free Software Foundation (FSF)

For your case, the following should work:

fullScriptPath="$(FullPath "$0")"

echo "$fullScriptPath"
2 Likes