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.
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?
for echo $0, ${0%/*} I get,
I also tried echo ${BASH_SOURCE[0]} for which I get,
I guess I would have to parse either output to get the script directory value I need. Are both of these options stable, meaning that the output format is predictable?
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:
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)