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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with pwd for script run by double click on script (MacOS High SIerra)
# 1  
Old 03-08-2019
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 03-08-2019
What be the output of
Code:
echo $0, ${0%/*}

in your script?
# 3  
Old 03-08-2019
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 03-09-2019
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:
# 5  
Old 03-09-2019
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; 03-09-2019 at 06:24 AM..
These 2 Users Gave Thanks to johnprogrammer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with shutdown command in script (MacOS High Sierra)

Hello, I have a backup script that runs an rsync backup to an external drive. I use the script frequently on Windows and Linux and have installed it on a Mac. The script has an option to run shutdown after the backup has completed. Since backup can take hours to run, this is an option that is... (10 Replies)
Discussion started by: LMHmedchem
10 Replies

2. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello, I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using, #!/bin/bash # main backup location, trailing slash included backup_loc="/Volumes/Archive_Volume/00_macos_backup/" # generic backup function function backup {... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

3. OS X (Apple)

If you run macOS High Sierra version 10.13.1, be sure to install today's update.

Some hackers found a security hole in macOS High Sierra and tweeted it to the world before telling Apple about the problem. You can see the details from PC Magazine's daily news here: Apple Releases Fix for MacOS High Sierra 'Root' Bug. The original story this morning was published before a patch... (6 Replies)
Discussion started by: Don Cragun
6 Replies

4. UNIX for Beginners Questions & Answers

Capture power button press on MacOs High Sierra?

Hello everyone! I'm developing a MacOs Application in python and I'm having some issues trying to find information related to the power button pressed event. I know that in Ubuntu 14.04 you can find information about it on the acpi folders, but I realized that here in Mac that process is... (0 Replies)
Discussion started by: xedge
0 Replies

5. UNIX for Dummies Questions & Answers

Pwd script

when user types "pwd.sh joe" at command line, it generates pwd as abc123 and when user types "pwd.sh jane" at command line, it generates pwd as abc123. pwd will change every 90 days. my pwd file will be always abc123 until i change joe/jane/bob pwd manually. (3 Replies)
Discussion started by: lawsongeek
3 Replies

6. Shell Programming and Scripting

getting pwd of script

how can i get the absolute path of whatever directory a script and/or command is in when it is run? i want to know the directory. say for instance, if i were to run the "who" command, i want to know exaclty where the who command is located. if a user ran a script, i want to know where there... (2 Replies)
Discussion started by: SkySmart
2 Replies

7. Red Hat

Unable to get $PWD value from script in /etc/init.d

Hi ppl hope to have your advice, i am run out of idea... I have 3 scripts: a.sh, b.sh, and c.sh a.sh resided in /etc/init.d b.sh and c.sh /opt/xSystem I intend to start my system with "service" command which will trigger my a.sh service a.sh start then. a.sh will trigger... (4 Replies)
Discussion started by: cielle
4 Replies

8. Shell Programming and Scripting

what would a script include to find CPU's %system time high and user time high?

Hi , I am trying to :wall: my head while scripting ..I am really new to this stuff , never did it before :( . how to find cpu's system high time and user time high in a script?? thanks , help would be appreciated ! :) (9 Replies)
Discussion started by: sushwey
9 Replies

9. Shell Programming and Scripting

script to change shell's pwd

Hi there, i was presented with a challenge that is beyond my current shell knowledge: how can you have a script that executed interactive will change your current working directory? Example (under MacOS): 1. start Terminal and my current working directory is my home folder 2. execute a... (3 Replies)
Discussion started by: gigagigosu
3 Replies

10. Shell Programming and Scripting

script for db user pwd

The script to get the db user password from LDAP does not work on AIX 5.3. It's using bash. Our current shell is /usr/bin/shell. How can i make changes to this script so that it can run on aix with current shell with out installing bash. We tried making but din't work. ... (0 Replies)
Discussion started by: noorm
0 Replies
Login or Register to Ask a Question