Source a tcsh script from bash - bash

I'm trying to execute a tcsh script in a bash environment. My regular environment is tcsh so the following command works for me:
source /usr/scripts/my_setup -t abs
But I need to execute this command from some third party software which runs the commands on bash shell. This script should set an environment variable $TMP.
I already tried all the suggested solutions from the previous threads and they all do not work.
What I tried:
tcsh -c "source /usr/scripts/my_setup -t abs ; exec bash"
tcsh -c "source /usr/scripts/my_setup -t abs ; bash"
The following steps:
echo "tcsh -c 'source /usr/scripts/my_setup -t abs'" > setup
chmod a+x setup
source setup
They all fail. And I understand why (everytime I use tcsh it opens a new shell). But I can't seem to understand how to source a tcsh script from bash.

Related

Run next command in script after invoking singularity shell

I have a shell script with multiple singularity commands in it
myscript.sh
#!/bin/sh/
singularity shell -B /home/user/Desktop/ /home/user/image/some_image.simg
/home/user/miniconda/activate my_env
cd /app/app_folder/scripts
ls -ash
when i run the script it get stuck after the shell is invoked.
singularity>
However I want the subsequent commands to run in the invoked shell. How do I go about this?
YOu are connecting to an interactive command shell inside a shell script, what you need is only execute the commands.
singularity exec ...
https://sylabs.io/guides/3.7/user-guide/cli/singularity_exec.html
So basically I had to replace shell with exec and save the subsequent commands in a different executable bash script newscript.sh which contains
#!/bin/sh
/home/user/miniconda/activate my_env
cd /app/app_folder/scripts
ls -ash
and then run myscript.sh
#!/bin/sh
singularity exec -B /home/user/Desktop/ /home/user/image/some_image.simg bash newscript.sh
This method will run newscript.sh in the singularity shell after the singularity shell is invoked in myscript.sh

Bash seems to ignore BASH_ENV when providing multiple commands to SSH

From Jenkins I try to send bash scripts to a host, where those bash scripts should be run. All those scripts have to be run in the same directory on the host. I do not want to provide this directory to each script, so I would like to cd into this directory and then run the script.
On the host the default shell is /bin/sh. In my .bashrc I set some variables, so I instruct bash to load my .bashrc with BASH_ENV. I run scripts with ssh user#host BASH_ENV='~/.bashrc' 'bash -s' < myscript.sh $appdir. This actually works, now when I remove $appdir from myscript.sh and ssh user#host BASH_ENV='~/.bashrc' "cd $appdir ; bash -s" < myscript.sh, variables from .bashrc are no longer available.
Following is a simple reproduction of this problem, only user, host and /path/to/git have been changed after the commands have been executed. git is added to $PATH in .bashrc, otherwise it is not available.
This works:
$ ssh user#host BASH_ENV='~/.bashrc' 'bash -c "type git" ; pwd'
git is /path/to/git
/home/user
This does not work:
$ ssh user#host BASH_ENV='~/.bashrc' 'cd ; bash -c "type git" ; pwd'
bash: line 0: type: git: not found
/home/user
There are different parts of SSH and Bash that I do not understand. I would be grateful for any information that would help me understand this problem.

Saving the result of an echo command in a shell script?

I am attempting to store the result of an echo command as a variable to be used in a shell script. Debian 4.19.0-6-amd64
The command works in terminal: echo $HOSTNAME returns debian-base, the correct hostname.
I attempt to run it in a shell script, such as:
#!/usr/bin/bash
CURRENT_HOSTNAME=`echo $HOSTNAME`
echo $CURRENT_HOSTNAME
I have tried expansion:
CURRENT_HOSTNAME=$(echo $HOSTNAME)
And just to cover some more bases, I tried things like:
CURRENT_HOSTNAME=$HOSTNAME
# or
CURRENT_HOSTNAME="$HOSTNAME"
# also, in case a problem with reserved names:
test=$HOSTNAME
test="$HOSTNAME"
Works great in the terminal! Output is as follows:
root#debian-base:/scripts# echo $HOSTNAME
debian-base
root#debian-base:/scripts# TEST_HOSTNAME=$HOSTNAME
root#debian-base:/scripts# echo $TEST_HOSTNAME
debian-base
root#debian-base:/scripts# TEST_TWO_HOSTNAME=$(echo $HOSTNAME)
root#debian-base:/scripts# echo $TEST_TWO_HOSTNAME
debian-base
As soon as I run the script (as above):
root#debian-base:/scripts# sh test.sh
root#debian-base:/scripts#
What am I doing wrong?
You are using bash as your terminal. Bash has the variable $HOSTNAME set. You run your script with sh. sh does not have a $HOSTNAME.
Options:
bash test.sh
Or run it as a program:
chmod +x test.sh
./test.sh
But I think you need to change your first line to:
#!/bin/bash
As I don't think bash is installed in /usr/bin in most cases. But you need to try. To figure out where bash is installed use which bash
Another option is to use the hostname binary:
CURRENT_HOSTNAME=$(hostname)
echo $CURRENT_HOSTNAME
Which works in both bash and sh.
You can start sh by just running sh. You will see it has a bash-like terminal. You can try to do echo $HOSTNAME. It will not show, because it's not there. You can use set to see all the variables that are there (as sh does not have tab completion it's harder to figure out).

How do I execute a shell builtin after executing a shell automatically without using profile and RC files?

I want to execute my shell with the environment I provide as the command line arguments. For that I have a script that ends with exec zsh -d -f after setting all the variables I want, which gives me a new shell with all variables set.
e.g:
export MY_SESSION="$1"
cd $2
export PS1="$3; "
exec zsh -d -f
My issue is that, I also want to execute bindkey -e on the new shell before it is made available. How do I do that?
I managed to do this with expect. Here is what I did.
#!/usr/bin/env expect
spawn /bin/zsh -d -f
send "source ./bin/activate\r"
send "export PS1='*| '\r"
send "bindkey -e\r"
interact
This successfully sends the bindkey -e on the produced zsh shell.

Running command as login shell without starting a new shell?

I'm trying to see what the output of a command would be if I were in a login shell, without having to go into a login shell. I've tried several variations of
zsh --login -c "alias"
But none of my aliases get shown; are --login and -c incompatible?
To test the difference between zsh --login -c "alias" and a normal login shell, you can/should add the -x option to see what the shell is up to.
When I run zsh -x --login -c "alias", then it processes /etc/zprofile.
When I run zsh -x --login, then it processes /etc/zprofile and /etc/zshrc.
I don't normally use zsh, so I don't have any personalized profile or start up file for it, but it seems plausible that it might look for (but, in my case, not find) ~/.zprofile and ~/.zshrc too.
I created trivial versions of those files:
$ echo "echo in .zprofile" > ~/.zprofile
$ echo "echo in .zshrc" > ~/.zshrc
and sure enough, they're processed. Further, the -c command with --login processed the .zprofile but did not process the .zshrc file.
Thus, using -c "alias" after the --login suppresses the processing of /etc/zshrc and ~/.zshrc. If you want those executed even so, you need to use something like:
zsh --login -c "[ -f /etc/zshrc ] && . /etc/zshrc; [ -f ~/.zshrc ] && . ~/.zshrc; alias"
Using -x to debug login processing is often informative.
It's nice that modern shells provide a command line option to induce login processing. I still have a program (which I don't use any more) that runs a login shell the old-fashioned way, by adding a - before the shell name in argv[0]. Thus, running -ksh would trigger login processing; the login program would run the login shell with the - at the start.

Resources