I want to use the script command to see my output both in the command line and also store it at the same time, as suggested here.

Now, I can do in my command line:

script source activate foo python my_file.py exit 

Quite a hassle if I have to do this often, so I thought I would write a bash script that automatizes this. I tried to literally paste these commands into a command line file, but it would wait for input after script.

Instead, here's how I thought of implementing this in a bash script (such that I would just run the script and it would do it all:

#!/bin/bash script ../output.txt -c ' /home/foo/anaconda3/condabin/conda activate myenv3 && python myfile.py' 

And this gives me a CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

Why am I not using directly "source activate foo" or ". activate foo" as I would usually do? Well, if I try that, I get bash: activate: No such file or directory

I understand that /bin/bash apparently is not configured to do this in Ubuntu -- what should be my way forward?

6

1 Answer

When you run commands with script -c, they are run in a noninteractive shell:

$ script ../output.txt -c 'echo "\$0 = $0 ; FLAGS = $-"' Script started, file is ../output.txt $0 = bash ; FLAGS = hBc Script done, file is ../output.txt 

The flags are different from the normal interactive shell (note the presence of the i flag):

$ echo "\$0 = $0 ; FLAGS = $-" $0 = -bash ; FLAGS = himBHs 

Although noninteractive, non-login shells read your ~/.bashrc, they likely processes it differently. In particular, the default ~/.bashrc that would have been copied from /etc/skel when your account was created starts with:

# If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac 

meaning that any conda related content that you added at the end of your ~/.bashrc will not get executed (and sourcing ~/.bashrc again won't help).

I don't know much about conda, but assuming it is capable of being run in such a shell, then one possible workaround would be the following:

  • remove the conda related portion of your ~/.bashrc to a separate file

  • source that file from your ~/.bashrc to keep the existing behavior for your interactive shell

  • source that file (instead of ~/.bashrc) first in your script -c ' ... ' command

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy