Linux script How to open new terminal and run commands in it - linux

I am writing a script which will take users input and then run following command in new terminal.
I am working on bigger "script", but for testing I created this little version of my problem.
newtermtest.sh
okegreen='\033[92m'
while true
read -p "Input Selection: " mainmenuinput
do
#find the name of instaled terminal then run new window of it and run bash in it !
#on my computer I have instaled x-terminal-emulator: so testing on it ---- NOT WORKING YET
case $mainmenuinput in
"0")
x-terminal-emulator -e "bash -c 'nmap -sV -sC 189.55.23.174 -vvv'"
;;
"1")
path=$(pwd)
x-terminal-emulator -e "bash -c 'cd $path;touch silk.txt'"
;;
*)
break;
esac
done
echo -e "${okegreen}leaving programe..."
I have found that something like x-terminal-emulator -e {command} should work, but when I run it I have the following error and the screen just flashes.
returned message:
QDir::exists: Empty or null file name
Then I would need something to keep the terminal up, I found --noclose or -hold tags, but I can't test it util I find the working command.
Do you have some experiences with opening new terminal from bash script ?
Iam opened for any hints.
Some temporary solution which works now:
x-terminal-emulator -e 'nmap -sV -sC 189.55.23.174 -vvv'
I used just -e to execute command and with nmap it is working now.
I will figure out other commands and I will try to put it in complex script with more types of terminals.
Thanks All for suggestions.

Related

Get bash script to open terminal

In Windows when I double-click a Batch script, it will automatically open a terminal window and show me what's happening. If I were to double-click a bash script in Linux, a terminal window does not open to show me what is happening; it runs in the background. I have seen that one can use one script to launch another script in a new terminal window with x-terminal-emulator -e "./script.sh", but is there any bash command I can put into the same (one) script.sh so that it will open a terminal and show me what's happening (or if I need to answer y/n questions)?
You can do something similar to what Slax
developers do in their bootinst.sh:
#!/usr/bin/env sh
#
# If you see this file in a text editor instead of getting it executed,
# then it is missing executable permissions (chmod). You can try to set
# exec permissions for this file by using: chmod a+x bootinst.sh
#
# Scrolling down will reveal the actual code of this script.
#
# if we're running this from X, re-run the script in konsole or xterm
if [ "$DISPLAY" != "" ]; then
if [ "$1" != "--rex" -a "$2" != "--rex" ]; then
konsole --nofork -e /bin/sh $0 --rex 2>/dev/null || xterm -e /bin/sh $0 --rex 2>/dev/null || /bin/sh $0 --rex 2>/dev/null
exit
fi
fi
# put contents of your script here
echo hi
# do not close the terminal immediately, let user look at the results
echo "Press Enter..."
read junk
This script would run correctly both when started in graphical
environment and in tty. It tries to restart the script inside
konsole and xterm and but if it doesn't find neither of them it
will simply run in the background.

vncserver bash script error in ubuntu 14.04.4 LTS

I use Ubuntu 14.04.4 LTS and installed vnc4server.
The basic script starting vncdesktops is under /etc/init.d/vncserver which is a bash script. This works fine for all users specified in /etc/vncserver/vncservers.conf with their arguments. But when a user is using csh instead of bash, the vncserver command doesn't work because of a syntax error in the vncserver script. The error occurs in the start() function which I show here.
start() {
. /lib/lsb/init-functions
REQ_USER=$2
echo -n $"Starting $prog: "
ulimit -S -c 0 >/dev/null 2>&1
RETVAL=0
for display in ${VNCSERVERS}
do
export USER="${display##*:}"
if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
echo -n "${display} "
unset BASH_ENV ENV
DISP="${display%%:*}"
export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
fi
done
}
When I give the command 'sudo service vncserver restart' the vncserver script runs and give me this error for a user using csh.
Starting VNC server: 8:test1 [: No match.
user 'test1' is using csh and its display number is 8. I can see this error is coming from the line
su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
and I understand what it's doing but I don't know why the bracket condition is giving me this error and that only for a user using csh.
Can anyone give me a clue?
I don't know exactly why, but I found my /bin/csh was linked as below.
/bin/csh -> /etc/alternatives/csh
So I installed tcsh (apt-get install tcsh) and made the link like this.
/bin/csh -> /bin/tcsh
Then the problem is gone!
The su command and the command executed by -c option is being processed in the su'ed user's shell which was /etc/alternatives/csh which probably could not handle the [ -f ] condition. and I gues the /etc/alternatives/csh is a kind of default csh linked to when there is no real full fledged csh(like tcsh).

Run a shell script in new terminal from current terminal

How do you run a shell script in a new terminal in Linux from a terminal like "start test.bat" in Windows, also it should be working in the console mode.
Here's a simple example to get you started:
To write a shell script, do this on your command prompt:
echo -e '#!/bin/sh\n echo "hello world"' > abc.sh
This writes:
#!/bin/sh
echo "hello world"
To a file called abc.sh
Next, you want to set it to executable by:
chmod +x abc.sh
Now, you can run it by:
./abc.sh
And you should see:
hello world
On your terminal.
To run it in a new terminal, you can do:
gnome-terminal -x ./abc.sh
or, if it's xterm:
xterm -e ./abc.sh
Here's a list of different terminal emulators.
Alternatively, you just run it in your current terminal, but background it instead by:
./abc.sh &
I came here wanting to figure out how to make a script spawn a terminal and run it self in it, so for those who want to do that I figured out this solution:
if [ ! -t 0 ]; then # script is executed outside the terminal?
# execute the script inside a terminal window with same arguments
x-terminal-emulator -e "$0" "$#"
# and abort running the rest of it
exit 0
fi
For gnome try this.
Replace ls with the command you want to run
gnome-terminal -x sh -c "ls|less"
I hope this is what you want
As of January 2020, the -e and -x option in gnome-terminal still run properly but throw out the following warnings:
For -e:
# Option “-e” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
For -x:
# Option “-x” is deprecated and might be removed in a later version
of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to
execute after it.
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
gnome-terminal -- /bin/sh -c '<your command>'
gnome-terminal -- ./<your script>.sh
I hope this helps anyone else presently having this issue :)

How to run screen executing a command over ssh with tty

I tried many things today to have ssh start a screen session which executes a command. The goal is to run a command on a remote machine and to be able to see the output and to detach and reattach latter. I want to do it from within a script without any interaction except detaching the screen session to close. No satisfying solution so far.
ssh -t ${host} "\
source ~/.bashrc; \
echo \"done.\"; \
cd \"$exedir\"; \
if [ \$? -ne 0 ]; then \
echo \"could not cd into directory\"; \
exit 1; \
fi; \
echo \"executing remotexe.sh ...\"; \
screen -S "remotexe" -t "remotexe" -R "nice -n$prio ./remotexe.sh ${exeparams[#]}";"
Some of the problems I encounter are related to the strange ways to pass commands to screen/ssh/bash which interfere with arguments and options (I don't quite understand why they do not use -- to interpret whatever follows as commands with arguments). The above version almost works. The remaining difficulty is that commands in remotexe.sh (in particular make) obviously miss exports and definitions from .bashrc. This is why I tried to include the source ~/.bashrc. I tried to add similar commands or explicit exports to remotexe.sh but it behaves as if it was executed by /bin/sh. If I do a conventional ssh login I can immediately run the remotexe.sh script without error. I also tried adding shell -$SHELL to my .screenrc.
Where is the mistake in this solution? How can I correct it?
I haven't tested your code at all, and will not vouch for the sanity of this, but you definitely have a quoting error. Try:
ssh -t ${host} "
source ~/.bashrc;
echo done.;
cd \"$exedir\" || exit 1;
echo executing remotexe.sh ...;
screen -S remotexe -t remotexe -R nice -n$prio ./remotexe.sh ${exeparams[#]};"

Linux screen push command error

#!/bin/sh
SERVER=$1
RCON=$2
echo "$SERVER"
echo "$RCON"
sudo -u flash screen -r $SERVER -X stuff "$RCON"`echo -ne '\015'`
Guys do you see something wrong with this code? The main concept in the script is to push a command to a opened screen session.
In particular, it's expected to execute a command into opened screen session.
The script echoes the two vars, but when I open the screen, the command $RCON isn't executed.
replaced
#!/bin/sh
with
#!/bin/bash

Resources