-1

I encountered a problem when I was trying to source a .bashrc.user file. It worked fine when I source the file in the terminal, it gave no complaints. But when I tried to do the same in my bash script, it gave some issues. here is how my code looks like:

comm -13 <(sort -u ~/.bashrc.user) <(sort -u ~/my_script/bashrc.user) > bashrc_diff
if [ -s bashrc_diff ]; then
    cat bashrc_diff >> ~/.bashrc.user
    source ~/.bashrc.user
    printf ".bashrc.user has been configured successfully!\n"
else
    printf ".bashrc.user is up-to-date!\n"
fi
rm bashrc_diff
source ~/.bashrc.user

So whenever I execute this code in my script, it gives and error and says that module: command not found. In the .bashrc.user file there are some module commands doing things like module add git/2.22.0 etc. As I mentioned before it works fine when I source it from terminal but complains in the script with the same type of command, what could be the issue here?

maoyi
  • 433
  • 1
  • 6
  • 12
  • What does `which module` in your terminal report, and what is the value of `PATH` when you run the script? – chepner Aug 11 '21 at 14:24
  • @chepner For `which module` it starts with `no module in ...()` then followed by a long path. For `PATH` variable it outputs a long path like this `/app/vbuild/RHEL7-x86_64/firefox/86.0:/app/vbuild/RHEL7-x86_64/python/3.6.6/bin:/app/vbuild/RHEL7-x86_64/vscode/1.44.2/bin:/app/vbuild/SLED11-x86_64/j2re/1.8.0_181/bin:/app/vbuild/SLED11- ....` – maoyi Aug 11 '21 at 14:31
  • 1
    Then it's not clear how `module` is found when you execute the code in your terminal. What *is* `module`, and what is it supposed to do? – chepner Aug 11 '21 at 14:40
  • What version of linux and what version of `which` ? I've not seen one that has that output. What is output of `type module` from terminal? – jhnc Aug 11 '21 at 15:35
  • Impossible to help without seeing all details. Show the bashrc file, the exact output, what `module` is (`which module`, `file module`). – Nic3500 Aug 12 '21 at 02:21
  • @chepner The `module` is a defined alias to add some tools in our environment i guess? In my case, I added it to add things like newer python version, newer git version etc. The syntax is simply `module add git/2.22.0` – maoyi Aug 12 '21 at 08:31
  • @jhnc The linux version is Red Hat 7 and `which` version is `GNU which v2.20`. And the `type module` output is like this ```module is a function module () { { eval `/app/modules/0/bin/modulecmd bash "$@"` } 2>&1 }``` – maoyi Aug 12 '21 at 08:38
  • @Nic3500 The bashrc.user file only contains some `module add ` and `alias` commands to setup things. I cannot show it coz of confidentiality reasons, but it's just like that. When I tried `which module` it says there is no module at all in some paths. – maoyi Aug 12 '21 at 08:43

1 Answers1

1

When you are using the terminal, you are running an instance of bash which has been initialised a certain way: Startup files will have been executed to configure your environment.

One of those startup files has caused a shell function module to be defined.

According to your comment, this function is defined as:

module(){
    { eval `/app/modules/0/bin/modulecmd bash "$@"` } 2>&1
}

To stop seeing errors when you run your script, you could look in the startup files for the place that defines module and then run the same commands at the beginning of your script.

Alternatively, you could try copying just the module function definition above into your script. However, this may lead to other errors later if the code which defines module does additional setup that you do not replicate.

As well as this, you should note that rearranging the lines of your ~/.bashrc with sort as you are doing only makes sense if the content is solely standalone lines that can be run in any order.

jhnc
  • 11,310
  • 1
  • 9
  • 26
  • Yeah the lines in `bashrc` are all solely standalone since they are mostly alias and module add commands. I also found out where the issue is, thanks to your `startup files` because my script apparently fails to convert the current existing `tcsh` into `bash`. So that's why it doesn't recognize the simple bash comamnds. – maoyi Aug 12 '21 at 14:43
  • Although the answer doesnt help me to solve the problem directly, but the link you provided was very helpful, and hinted me to check up on if it's an interactive shell or not that i am using. SO I will accept this answer. – maoyi Aug 12 '21 at 14:44