Execute multiple command onto same terminal using bash script - bash

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/

Related

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"'

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

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"

Bash brace expansion not working on Dockerfile RUN command

I'm running the following RUN command in my Dockerfile, expecting a "logs" directory to be created under each of the listed subdirectories:
RUN mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs
But when I check the image, I see a directory literally called "{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}" created under /opt/seagull, instead of brace expansion taking place.
What could I be doing wrong?
You're not using brace expansion, because you're not using Bash. If you look at the documentation for the RUN command:
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
And also:
Note: To use a different shell, other than ‘/bin/sh’, use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]
So, just change the command to use the exec form and explicitly use a Bash shell:
RUN [ "/bin/bash", "-c", "mkdir -p /opt/seagull/{diameter-env,h248-env,http-env,msrp-env,octcap-env,radius-env,sip-env,synchro-env,xcap-env}/logs" ]
If /bin/bash is available in your image, you can change the shell that the docker build system uses to execute your RUN command, like this:
SHELL ["/bin/bash", "-c"]
Now, your RUN command should work unchanged.

Embedded terminal startup script

I usually use bash scripts to setup my environments (mostly aliases that interact with Docker), ie:
# ops-setup.sh
#!/bin/bash
PROJECT_NAME="my_awesome_project"
PROJECT_PATH=`pwd`/${BASH_SOURCE[0]}
WEB_CONTAINER=${PROJECT_NAME}"_web_1"
DB_CONTAINER=${PROJECT_NAME}"_db_1"
alias chroot_project="cd $PROJECT_PATH"
alias compose="chroot_project;COMPOSE_PROJECT_NAME=$PROJECT_NAME docker-compose"
alias up="compose up -d"
alias down="compose stop;compose rm -f --all nginx web python"
alias web_exec="docker exec -ti $WEB_CONTAINER"
alias db="docker exec -ti $DB_CONTAINER su - postgres -c 'psql $PROJECT_NAME'"
# ...
I'd like them to be run when I open the embedded terminal.
I tried Startup Tasks but they are not run in my terminal contexts.
Since I have a dedicated script for each of my projects, I can't run them from .bashrc or other.
How can I get my aliases automatically set at terminal opening ?
Today I'm running . ./ops-setup.sh manually each time I open a new embedded terminal.
You can create an alias in your .bashrc file like so:
alias ops-setup='bash --init-file <(echo '. /home/test/ops-setup.sh'; echo '. /home/test/.bashrc')'
If you call ops-setup, it will open up a new bash inside your terminal, and source .bashrc like it normally would, as well as your own script.
The only way I see to completely automate this is to modify the source code of your shell, e.g. bash, and recompile it. The files that are sourced are hardcoded into the source code.

Script to change the directory path

I was trying the below program,
This is a simple script, to cd into a folder
#! /bin/bash
cd /root/
But this below command , doesnt get into the folder
EDITED
#!/bin/bash
alias ex="cd /fs/fm"
alias ex1="source setenv"
alias ex2="cd /fs/fm/tests"
alias ex3="runtest"
To get into /root/ you should make sure that you have permissions. It's accessible if you're running as root itself but if you're running as a normal user you should consider becoming root first. One way is to use sudo:
sudo bash script.sh
And again, make sure your script is in UNIX format. Certainly you can't change to /root/\r.
sed -i 's|\r||' script.sh
dos2unix script.sh
This will never work The script you're running is a separate process, when it finishes you get back to the original environment (cwd, enviroment variables, etc...).
Create an alias:
alias r="cd /root"
or execute the script within your shell:
. myscript
Note: . is a synonym for source.

Resources