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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 2 Weeks Ago
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.

Code:
#!/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
# 2  
Old 2 Weeks Ago
What be the output of
Code:
echo $0, ${0%/*}

in your script?
# 3  
Old 2 Weeks Ago
Quote:
Originally Posted by RudiC
What be the output of
Code:
echo $0, ${0%/*}

in your script?
for echo $0, ${0%/*} I get,
Code:
/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,
Code:
/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
Code:
 /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
# 4  
Old 2 Weeks Ago
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:
Code:
#!/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
This User Gave Thanks to Don Cragun For This Post:
LMHmedchem (2 Weeks Ago)
# 5  
Old 2 Weeks Ago
Here is POSIX shell compliant code, that returns the full path of a file or directory.

Code:
#!/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:

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

echo "$fullScriptPath"


Last edited by johnprogrammer; 2 Weeks Ago at 05:24 AM..
These 2 Users Gave Thanks to johnprogrammer For This Post:
LMHmedchem (2 Weeks Ago) Neo (2 Weeks Ago)
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Terminal running bash/rsync script does not close with exit (MacOS High SIerra) LMHmedchem Shell Programming and Scripting 12 2 Weeks Ago 07:09 AM
If you run macOS High Sierra version 10.13.1, be sure to install today's update. Don Cragun OS X (Apple) 6 12-01-2017 01:05 PM
Capture power button press on MacOs High Sierra? xedge UNIX for Beginners Questions & Answers 0 10-12-2017 05:04 PM
Pwd script lawsongeek UNIX for Dummies Questions & Answers 3 02-08-2013 11:50 AM
CURL Button Click Issue js0505 Shell Programming and Scripting 5 12-19-2012 11:19 AM
getting pwd of script SkySmart Shell Programming and Scripting 2 10-22-2012 12:41 PM
Script to read a log file and run 2nd script if the dates match SarwalR Shell Programming and Scripting 1 09-18-2012 11:46 AM
Unable to get $PWD value from script in /etc/init.d cielle Red Hat 4 04-30-2012 09:32 AM
what would a script include to find CPU's %system time high and user time high? sushwey Shell Programming and Scripting 9 04-26-2012 11:44 AM
script to change shell's pwd gigagigosu Shell Programming and Scripting 3 12-20-2011 01:19 PM
How to pass the pwd to an export command through script? srini0603 Shell Programming and Scripting 3 10-17-2011 01:34 AM
script for db user pwd noorm Shell Programming and Scripting 0 08-12-2011 03:03 PM
pwd & cd commands not working in shell script perk_bud Shell Programming and Scripting 9 04-07-2008 07:11 AM
Left click select,right click copy new2ss UNIX for Dummies Questions & Answers 1 06-11-2007 11:00 AM