How to open a new terminal and run two linux commands? - bash

I have create a .sh script, with option to open terminal and that same terminal run 2 commands.
I know to open a new window or tab and run a command but when I put && I receive error like this by using this command:
Commands:
gnome-terminal --tab -- "ls && clear"
Error:
There was an error creating the child process for this terminal
Failed to execute child process “ls && clear” (No such file or directory)
My OS is Linux(Ubuntu)

gnome-terminal is expecting a command name. It looks for a command literally called ls && clear but can't find it. You need to run it in a shell:
gnome-terminal --tab -- bash -c "ls && clear"

Related

How to run shell script as a program without terminal in XFCE

I have below sh file
log.sh
#! /bin/sh
cd /home/andrew/Documents/
ls
I have enabled it to run as program. When running in terminal, it runs fine but I want to run it by double clicking it. How can I do it in XFCE.
I have tried executing below command as well but still it doesn't work:
xfconf-query --channel thunar --property /misc-exec-shell-scripts-by-default --create --type bool --set true

Why does terminal stop after run "bash" in a .bat file (WSL) and how to run it properly?

I would like to automatize a process running a .bat file but using the bash terminal in my WSL2. These are my commands in my .bat:
bash
cd ~/Dev/Folder
nvm use 14.15.0
npm start
But the .bat stops after running "bash". I also tried with bash && cd ~/Dev/Folder && nvm use 14.15.0 && npm start and also replacing "bash" with "wsl", but same result.
Maybe I'm doing something wrong, so I would appreciate some help with this.
bash starts a new Bash shell and waits for it to exit, like for any other terminal command. The subsequent commands are not redirected to Bash - they'll be executed once the Bash process exits.
To run commands within Bash, use the -c flag:
bash -c "cd ~/Dev/Folder && ls && nvm use 14.15.0 && npm start"
Also see What does 'bash -c' do?.

How to fix "Failed to execute child process source (no such file or directory)"

I am trying to run a script in order to source my catkin workspace prior to running a driver.
This is the bash I am trying to run:
#!/bin/bash
gnome-terminal -e sudo systemctl start firmwared.service
sphinx /opt/parrot-sphinx/usr/share/sphinx/drones/bebop2.drone &
gnome-terminal -e source ~/bebop_ws/devel/setup.bash
roslaunch bebop_driver bebop_node.launch ip:=10.202.0.1
I get failed to execute child process source (no such file or directory) when running my bash
Seems like the problem was the quoting, it should look like this
gnome-terminal -e "bash -c 'source ~/bebop_ws/devel/setup.bash;roslaunch bebop_driver bebop_node.launch ip:=10.202.0.1'"
IF you want to use GUI on windows and failed then just use next command:
sudo apt-get install dbus-x11
source is built into the shell, so you would have to do something like
gnome-terminal -e 'bash -c "source ~/bebop_ws/devel/setup.bash"'

Execute multiple command onto same terminal using bash script

I am trying to run a bash script which contains few commands to execute. I want to open a terminal and execute multiple commands into it. I have written commands to set the directory path and want to make a folder there.
Code :
gnome-terminal --working-directory=/var/run/
gnome-terminal -e "bash -c \"sudo mkdir sphinxsearch; exec bash\""
Here, There are 2 problems :
1) Two separate terminal are opened that I don't want. I need only a single terimal where I will execute my commands.
2) sudo mkdir sphinxsearch folder is created at the default path from where I am executing my bash script. I need to create a folder inside /var/run/
Each invocation of gnome-terminal will open a separate terminals.
Try this:
gnome-terminal --working-directory=/var/run/ -e "bash -c \"sudo mkdir sphinxsearch; exec bash\""
Here i am combining both options in a single invocation of gnome-terminal
sudo mkdir /var/run/sphinxsearch;
will create the folder in /var/run/

Open gnome terminal programmatically and execute commands after bashrc was executed

I try to build a little script to start my development environment. For that task I try to open a gnome terminal with several tabs where automatically the rails server and autotest is started.
But
gnome-terminal --tab -e "rails server" --tab --tab
does not work ("error creating the child process").
Also
gnome-terminal --tab -e "bash -c \"rails server\"" --tab --tab`
does not work.
Any suggestions how to solve that problem?
Here is a nice trick we worked out at Superuser
Add a eval "$BASH_POST_RC" to the end of your .bashrc
Set the BASH_POST_RC environment variable for each tab to that command you like to execute, e.g.: gnome-terminal --working-directory="/home/zardoz/projects/my_rails_app" --tab -e 'bash -c "export BASH_POST_RC=\"rails server\"; exec bash"' --tab -e 'bash -c "export BASH_POST_RC=\"autotest\"; exec bash"'
#Gilles: Thanks for that solution!
Stab in the dark: create shell scripts for each command you want to run in a tab, make them executable, and invoke them by absolute path, e.g. put this in /home/zardoz/bin/railsstart
#! /bin/sh
exec rails server
chmod +x it, and then do
gnome-terminal --tab -e /home/zardoz/bin/railsstart --tab --tab ...
If that doesn't work, the next thing I would try is sticking strace -f -o /tmp/trace.log on the beginning of the command, letting it fail, and then digging through trace.log to find out which system call actually failed and why (there'll be a tremendous amount of junk in there - read from the end backward and look for all-capitalized code phrases starting with E, like "ENOEXEC", "ENOENT", "EPERM", sort of thing.)
EDIT: Here's how you pull in all the .bashrc settings in one of these scripts:
#! /bin/bash
. ~/.bashrc
exec rails server
Caution: you may need to adjust your .bashrc so that it doesn't do certain things that only work in a "real" interactive shell session. Don't worry about this unless you get strange errors before rails starts.
I'm assuming the error arises because PATH is not set at the time gnome-terminal tries to run rails.
Why not use the full path to the rails server, or create a script that sets the PATH variable?
Already replied, but just in case, check out this gem that automates the terminal on KDE, OSX and Gnome desktops.
for ubuntu 16.04
press Ctr+Shift+T
this will open a new tab in the same window. additionally a button for adding more tabs will appear next to the right most tab.

Resources