Sponsored Content
Top Forums Shell Programming and Scripting Issue with pwd for script run by double click on script (MacOS High SIerra) Post 303031999 by johnprogrammer on Saturday 9th of March 2019 05:17:23 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Parse::DebianChangelog::ChangesFilters(3pm)		User Contributed Perl Documentation	       Parse::DebianChangelog::ChangesFilters(3pm)

NAME
Parse::DebianChangelog::ChangesFilters - filters to be applied to Debian changelog entries SYNOPSIS
DESCRIPTION
This is currently only used internally by Parse::DebianChangelog and is not yet documented. There may be still API changes until this module is finalized. SEE ALSO
Parse::DebianChangelog AUTHOR
Frank Lichtenheld, <frank@lichtenheld.de> COPYRIGHT AND LICENSE
Copyright (C) 2005 by Frank Lichtenheld 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA perl v5.10.1 2011-04-04 Parse::DebianChangelog::ChangesFilters(3pm)
All times are GMT -4. The time now is 02:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy