0

I want to write and test scripts that do not depend on any of the personal environment variables accessible to me at the command line. For instance, each time I open a new command line window on my macbook pro, $PATH is updated by having my personal bin directory prepended to it --- as well as several other directories, such as

/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin

This occurs apparently because my $HOME/.profile is automatically sourced when a window opens up. In addition, a large number of other variables are defined at login because my personal .bashrc is sourced automatically.

Ultimately, I want to be able to run a script as a cron job --- using crontab -l. But if the script depends on my personal configuration, or if it tries to access a script that lives in my personal bin, it will halt with error.

So I write a script, test it at the command line, set up a cron job to run it; and when the time comes, it halts with error, because it turns out that the script depended on one of my personal environment variables.

Currently, then, the only way I know to test such a script is to set up a cron job --- say, five minutes from now---and then wait for the cron job to run. It's clumsy and slow.

Is there a direct and immediate way to run a script so that it does not know anything from $HOME/.profile?

The script I tested this with:

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper qw(Dumper);
$Data::Dumper::Sortkeys = 1;
print Dumper {%ENV};

The following command

> jaw20210419test.pl | wc
    2532    3098  118462

produces many lines of output, as you see, because of all that I define in my .bashrc. With the following,

> sudo jaw20210419test.pl | wc
Password:
      20      59     831
> 

the output is much less, but it still has an updated $PATH including my personal bin. I do not understand this, since the $PATH gets updated by $HOME/.profile, and that file sources my personal $HOME/u/kh/.bashrc.

If however I run this with a cron job, then it does not have an updated $PATH. The script says

$VAR1 = {
          'HOME' => '/Users/kpr',
          'LOGNAME' => 'kpr',
          'PATH' => '/usr/bin:/bin',
          'PWD' => '/Users/kpr',
          'SHELL' => '/bin/sh',
          'SHLVL' => '1',
          'USER' => 'kpr',
          'VERSIONER_PERL_VERSION' => '5.18',
          '_' => '/Users/kpr/u/kh/bin/jaw20210419test.pl',
          '__CF_USER_TEXT_ENCODING' => '0x1F5:0x0:0x0'
        };

This is the configuration I want to test the script with. Note that it knows who I am --- $USER --- and where $HOME is.

By contrast, two other proposed solutions, from How to start a shell without any user configuration?, do not even know know where $HOME is:

> echo $cdbin # personal environment variable
/Users/kpr/u/kh/bin
> env -i perl $cdbin/jaw20210419test.pl 
$VAR1 = {
          'VERSIONER_PERL_VERSION' => '5.18',
          '__CF_USER_TEXT_ENCODING' => '0x1F5:0x0:0x0'
        }; 
> env --noprofile --norc  perl $cdbin/jaw20210419test.pl 
env: illegal option -- n
usage: env [-iv] [-P utilpath] [-S string] [-u name]
           [name=value ...] [utility [argument ...]]
> echo $SHELL
/bin/bash
> 
> env -i sh -c  $cdbin/jaw20210419test.pl 
$VAR1 = {
          'PWD' => '/Users/kpr/u/sng/2021/FR-Wegelin-TO-stackoverflow',
          'SHLVL' => '1',
          'VERSIONER_PERL_VERSION' => '5.18',
          '_' => '/Users/kpr/u/kh/bin/jaw20210419test.pl',
          '__CF_USER_TEXT_ENCODING' => '0x1F5:0x0:0x0'
        };

so I am still lost.

Jacob Wegelin
  • 1,174
  • 10
  • 15

0 Answers0