Bash - open a new terminal tab then execute command - bash

The following bash script is suppose to open 2 new terminal tabs then execute respective commands:
mate-terminal --tab -e "cd ~/ece344/root; sys161 -w kernel" --tab -e "cd ~/ece344/root; cs161-gdb kernel"
The script does open 2 new tabs however both tabs display the following error:
There was an error creating the child process for this terminal
Failed to execute child process "cd" (No such file or directory)
Ps. The answer should work with mate-terminal.

I don't have mate installed but I would try :
mate-terminal --tab -e "/bin/bash -c 'cd ~/ece344/root; sys161 -w kernel'" --tab -e "/bin/bash -c 'cd ~/ece344/root; cs161-gdb kernel'"
The idea is that "-e" would want to execute a command that probably run inside the window instead of a default shell, so from the error I understand that "cd" is not a real program in an expected location (since 'cd' is in the PATH shouldn't be a problem.
So my example would provide a full path to a shell "/bin/bash" that would then execute the commands you want.

Related

Open git bash shell window, execute command and remain after term signal

I have a batch file that sets up my environment by opening a number of "git bash" shell windows. This works perfectly apart from one annoying feature where if you press Ctrl C (or send any other Term signal) the whole bash window will close.
I want the window to behave as if it has been opened normally and so when it receives a term signal it goes back to the bash prompt.
Here is the current contents of my setup.bat file:
C:
cd \project\
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i -l -c "source ali.sh && mvn spring-boot:run"
cd \project2\
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login
Note that the first start command runs maven and when i want to restart the command (press Ctrl+C) it closes the whole window.
The second start command creates a new bash window in that directory which works like a normal bash window even with Ctrl+C, BUT I want it to have run a command at the start.
Is this possible?
Many thanks for all your help
Something like this might work for you:
C:
cd \project\
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i -l -c "sh -c 'source ali.sh && mvn spring-boot:run; exec sh'"
The trick is wrapping the command in:
sh -c '...; exec sh'
Where sh is your shell, might be bash.
You might actually to able to just suffix each command with: exec sh, eg:
start "" "%SYSTEMDRIVE%\Program Files (x86)\Git\bin\sh.exe" --login -i -l -c "source ali.sh && mvn spring-boot:run; exec sh"

Why I am getting different screen session when opened from a bash script

I am opening a new screen in detached mode from the following one-liner bash script:
screen -d -m -S screenName
But the terminal prompt of screen displays bash-3.2$ instead of my regular prompt atul.vaibhav#Mac$.
When I execute the same command directly from terminal, I get normal expected prompt. Why am I getting a different prompt when opening a screen from bash script?
I am executing my bash script in this manner:
./myScript.sh
Since the synopsis from man screen gives:
screen [ -options ] [ cmd [ args ] ]
and you want a login Bash shell, you should probably run:
screen -d -m -S screenName -- bash -l
That said, while the command ran and ps claimed to have created a bash -l somewhere, I couldn't see the window.
Try this:
bash -l ./myScript.sh
or
bash -i ./myScript.sh
From man bash:
-l: Make bash act as if it had been invoked as a login shell.
-i: If the -i option is present, the shell is interactive.

How to execute multiple commands after opening a new terminal tab

I need to open a new terminal tab and execute multiple commands on it, how can I do it. I have already tried the following,
gnome-terminal --tab -t "X" -e "cd ~/Desktop/terminal_test;mkdir test"
Here I need to cd into a new directory and create a new folder.
Try this:
gnome-terminal -x bash -c "cmd1; cmd2; …cmdN; exec bash"
You can use gnome-terminal. The script below will open 3 tabs running the respective commands..
tab="--tab"
cmd01="bash -c 'ls';'pwd';bash"
foo=""
foo+=($tab -e "$cmd01")
gnome-terminal "${foo[#]}"
exit 0

BASH get error code in parent terminal from child terminal?

In a bash script I start a new terminal with a command that gives an error. However I don't seem to be able to grab that error code:
#! /bin/bash
gnome-terminal -x bash -c "cat dksdamfasdlm"
echo $?
Output:
0
So I get the error code of the gnome-terminal command instead of the cat one. One suggestion I got, was to make a file with the code and read that from the parent bash. The problem is that I still seem to not be able and read the error code even that way:
#! /bin/bash
gnome-terminal -x bash -c "cat dksdamfasdlm; echo $?; sleep 2"
Output (on new terminal):
cat: dksdamfasdlm: No such file or directory
0
Why is that? Some suggestion on how to solve this? I just want to somehow grab the error in the new terminal from the parent bash.
It seems GNOME Terminal exits immediately after starting, which is obvious if you run for example gnome-terminal -x sleep 10. Since it doesn't wait for the command to finish, there's no way the return code will be that of the command. I could find no option in gnome-terminal --help-all to keep the process in the foreground.
Regarding your second question, you've double-quoted the command, so $? is expanded before running it. This should work:
gnome-terminal -x bash -c 'cat dksdamfasdlm; echo $?; sleep 2'
PS: The -x option is not documented in GNOME Terminal 3.8.4's gnome-terminal --help-all, various references don't help much, and there's no good explanation for why there's a -e option with identical semantics and different syntax.

How I can write a command in a terminal which is running a program and I open from a script

I already know how to open a terminal in a bash with gnome-terminal and execute a program:
gnome-terminal -e ./OpenBTSCLI
But I also need that once open that program in the new terminal, write another command inside.
When a I tried to use echo, the message appear in the terminal where I run the bash.
I tried: gnome-terminal -e "bash -c './OpenBTSCLI && echo message'" that I find online but its not working, it only do the first part.
Anyone have an idea of how to resolve this? Thank you
I think it does the second command as well, but the new terminal closes as soon as the command's finished, so you don't see it. I reversed the order of quotes and added a 1s sleep at the end to allow seeing the echo.
gnome-terminal -e 'bash -c "./OpenBTSCLI && echo message && sleep 1"'

Resources