On Sat, Aug 26, 2023 at 12:09:57PM -0400, Tom Browder wrote:
> Excellent mind-reading, Greg! So to use your line I will put in that dir:
>     "cd /required-dir || exit"
> 
> Thanks so much.  And thanks to all others who responded.

If you're running bash, the safest way to find your current working
directory is capturing the output from /bin/pwd.  Symlinked directories
can surprise you:

    me$ cd

    me$ ls -ldF today
    lrwxr-xr-x  1 me mis   18 Aug 26 00:03 today@ -> notebook/2023/0826

    me$ cd today

    me$ pwd
    /home/me/today

    me$ /bin/pwd
    /home/me/notebook/2023/0826

    me$ echo $PWD
    /home/me/today

If you want to know why you had an early exit:

    me$ cat try
    #!/usr/bin/env bash
    # try: test logging.

    export PATH=/usr/local/bin:/bin:/usr/bin
    set -o nounset      # check for unbound variables.
    tag=${0##*/}
    umask 022

    # Test file descriptor 2 for interactive or cron use.
    test -t 2
    case "$?" in
        0) logmsg () { echo "$(date '+%F %T') $tag: $@"; } ;;
        *) logmsg () { logger -t $tag "$@"; }              ;;
    esac

    warn () { logmsg "WARN: $@" ; }
    die ()  { logmsg "FATAL: $@"; exit 1 ; }

    # Real work starts here.
    case "$#" in
        0)  die "need a directory" ;;
        *)  dir="$1" ;;
    esac

    test -d "$dir" || die "$dir: not a directory"
    cd "$dir"      || die "$dir: cannot cd"
    cwd=$(/bin/pwd)

    logmsg "start working in $cwd"
    exit 0

On FreeBSD, you can use "daemon" to run something detached from the
controlling terminal, which simulates running a cron job:

    me$ ls -ldF /etc /var/authpf
    drwxr-xr-x 27 root wheel  120 26-Aug-2023 07:55:02 /etc/
    drwxrwx---  2 root authpf   2 05-Jul-2019 00:45:45 /var/authpf/

    me$ ./try /etc
    2023-08-26 18:31:54 try: start working in /etc

    me$ daemon -f $PWD/try /etc
    me$ daemon -f $PWD/try /var/authpf

    me$ tail -2 /var/log/syslog
    Aug 26 18:19:17 myhost try: start working in /etc
    Aug 26 18:19:19 myhost try: FATAL: /var/authpf: cannot cd

Hope this helps.

--
Karl Vogel                      I don't speak for anyone but myself.

Oh, my darlin' had bronchitis and she barfed up half a lung,
what came up looked quite amazing when she rolled it on her tongue.
                               --sung to the tune of "My Darling Clementine"

Reply via email to