outputting script results into terminal [SOLVED]
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
outputting script results into terminal [SOLVED]
This question has two parts to it. I've typed out a simple script that I'll run (not every time) when I boot into the GUI, but I'm running into a few hiccups. First part is that none of the outputs of these commands show up in terminal, not even cd. So I was wondering what I need to add to the script in order for the outputs of said commands (red) to be displayed in terminal. The other stuff (blue) should probably remain hidden.
#!/bin/sh
TERM=linux
gnome-terminal
export TERM
ifconfig wlan1 up
wait
ifconfig
wait
cd /home/user/documents
ls
The second thing is that I have terminal launched in workspace 3 by default on startup (devilspie), can I have the above script somehow output into that terminal when I double click the file?
#!/bin/sh
TERM=linux
gnome-terminal
export TERM
ifconfig wlan1 up
wait
ifconfig
wait
cd /home/user/documents
ls
The second thing is that I have terminal launched in workspace 3 by default on startup (devilspie), can I have the above script somehow output into that terminal when I double click the file?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: outputting script results into terminal
Normally, to see the output of a script, you would do one of the following:
1. Open a terminal, and run your script from there.
2. From Nautilus (the file manager) double-click the script, and choose Run as Script (IIRC). This shows you the output, but the terminal closes once the script finishes. So add a line at the end of your script to pause for user input:
3. Automatically run the script, from Startup Programs for example, and have it save its output to a logfile. Insert a line after #!/bin/bash as following:
Replace your_logfile with full path to a logfile.
I see on the third line of your script you run gnome-terminal. This will open a new, empty, terminal and the script will just continue to run from where it started. If you want to have the output of the script in the new terminal you launched, you will have to split it up in two scripts (or do one of the above three).
Example files below, replace /home/user with actual path to your homedirectory or where you place the scripts.
First file: /home/user/start.sh
Second file: /home/user/justdoit.sh
1. Open a terminal, and run your script from there.
2. From Nautilus (the file manager) double-click the script, and choose Run as Script (IIRC). This shows you the output, but the terminal closes once the script finishes. So add a line at the end of your script to pause for user input:
Code: Select all
read -p "Press enter to continue..." -s
Code: Select all
exec > your_logfile 2>&1
I see on the third line of your script you run gnome-terminal. This will open a new, empty, terminal and the script will just continue to run from where it started. If you want to have the output of the script in the new terminal you launched, you will have to split it up in two scripts (or do one of the above three).
Example files below, replace /home/user with actual path to your homedirectory or where you place the scripts.
First file: /home/user/start.sh
Code: Select all
#!/bin/bash
TERM=linux
export TERM
gnome-terminal --command /home/user/justdoit.sh &
Code: Select all
#!/bin/bash
ifconfig wlan1 up
wait
ifconfig
wait
cd /home/user/documents
ls
Re: outputting script results into terminal
Code: Select all
#!/bin/sh
set -x
...
- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: outputting script results into terminal
You could also try:
Code: Select all
#!/bin/sh
# Make sure the script is running in a terminal window
tty -s
SUCCESS=$?
if [ $SUCCESS -ne 0 ]
then
gnome-terminal --command=$0
exit
fi
# Put the rest of your script after this line
...
...
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: outputting script results into terminal
Cool, tried it gnome-terminal doesn't work if you already have another terminal open, that also kills my "two scripts" solution Also, it doesn't pause after doing the commands.Pilosopong Tasyo wrote:You could also try:
Code: Select all
#!/bin/sh # Make sure the script is running in a terminal window tty -s SUCCESS=$? if [ $SUCCESS -ne 0 ] then gnome-terminal --command=$0 exit fi # Put the rest of your script after this line ... ...
- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: outputting script results into terminal
I don't understand what you meant, but just for sh!ts and giggles I wrote a short script following the above pattern:xenopeek wrote:...gnome-terminal doesn't work if you already have another terminal open...Also, it doesn't pause after doing the commands.
el-es.sh
Code: Select all
#!/bin/sh
# Make sure the script is running in a terminal window
tty -s
SUCCESS=$?
if [ $SUCCESS -ne 0 ]
then
gnome-terminal --command=$0
exit
fi
# Put the rest of your script after this line
ls -l
read -p "Press the enter key..." DUMMY
It also works while in a terminal window, as well as during standalone login, albeit it doesn't pop-up a separate terminal window (the if-then-fi statement gets bypassed).
The goal of the snippet of code is to make sure terminal output gets a chance to be seen when a script is run within the confines of the GUI. Naturally, if you run the script in a terminal window in the first place, it is no longer necessary to create another separate terminal window just for the sake of giving output when the main terminal window is already there to give output.
The read command at the end of the script should fix the problem with the terminal window getting closed too quickly. As an alternative, the sleep command can be used in lieu of read. Another suggestion is to change gnome-terminal's preferences and set it to hold the terminal open once the script returns control to the operating system.
I think the reason why the
Code: Select all
read -p "Press enter to continue..." -s
Code: Select all
#!/bin/sh
Code: Select all
#!/bin/bash
I didn't meant to step on you. I was reading the thread last night and saw your two-script solution. I thought to myself that maintaining a complementary script wasn't necessary when it can get more efficient by doing it inline. Hence the post.xenopeek wrote:...that also kills my "two scripts" solution
Hope this clarifies things.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: outputting script results into terminal
It doesn't show me the output prior to adding the line, still same result after adding that line. just a ~ $ command prompt.xenopeek wrote: 2. From Nautilus (the file manager) double-click the script, and choose Run as Script (IIRC). This shows you the output, but the terminal closes once the script finishes. So add a line at the end of your script to pause for user input:Code: Select all
read -p "Press enter to continue..." -s
tried with both #!/bin/sh and #!/bin/bash as Pilosopong Tasyo wrote.
set -x resulted in just a ~ $ command prompt as wellHabitual wrote:run the script manually, set -x will show you a more verbose output, every step of the way.Code: Select all
#!/bin/sh set -x ...
But doesn't -x remove the file's ability to execute? Cause I know that I had to chmod +x in order to make the file executable.
I have no idea what you did there, but I copied and pasted as you said with the same result as all the others.Pilosopong Tasyo wrote:You could also try:
Code: Select all
#!/bin/sh # Make sure the script is running in a terminal window tty -s SUCCESS=$? if [ $SUCCESS -ne 0 ] then gnome-terminal --command=$0 exit fi # Put the rest of your script after this line ... ...
I didn't think that it would matter, but in case it does... 32bit mint10 gnome?
also, the way I created this file was:
right click > new file > name "test"
in terminal > chmod +x test
then proceeded with the above script.
I tried adding a "mkdir poop" and it does, so it looks like the file is executing.
- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: outputting script results into terminal
I use Mint 9 (LTS) Gnome.I didn't think that it would matter, but in case it does... 32bit mint10 gnome?
Humor me for a moment, you copied and pasted everything literally? You do realize the ellipses (...) at the bottom of the code fragment is meant for you to put the actual commands there. I'm not trying to be patronizing, but everyone knows how prone to misinterpretation the English language is.I have no idea what you did there, but I copied and pasted as you said with the same result as all the others.
Code: Select all
#!/bin/sh
# Make sure the script is running in a terminal window
tty -s
SUCCESS=$?
if [ $SUCCESS -ne 0 ]
then
gnome-terminal --command=$0
exit
fi
# Put the rest of your script after this line
ifconfig wlan1 up
wait
ifconfig
wait
cd /home/user/documents
ls
read -p "Press the enter key..." DUMMY
Now, as far as executed commands not giving you any feedback, that's not uncommon. It is normal and standard convention that if a command does not return any message, then the command ran successfully. It will only provide feedback when necessary, such as if there was an error or it needs to provide output based on what was requested.
If you still want some feedback regardless if a command ran successfully or not, then it is possible. Apart from the set -x command mentioned earlier, there are at least two other ways:
(1) Depending on the command, you can add the -v switch. -v means verbose output, i.e. it will provide feedback as to what is going on. Not all commands support this switch, though. You will have to consult the man pages to find out. E.g.:
Code: Select all
administrator@dg31pr ~ $ mkdir $HOME/no-feedback
administrator@dg31pr ~ $ mkdir -v $HOME/with-feedback
mkdir: created directory `/home/administrator/with-feedback'
administrator@dg31pr ~ $
Code: Select all
#!/bin/sh
# The following will succeed
mkdir $HOME/this-is-allowed
SUCCESS=$?
if [ $SUCCESS -eq 0 ]
then
echo "mkdir successful" # <-- this will show on screen
else
echo "mkdir unsuccessful"
fi
# Whereas the following will fail. You can't create a directory
# from the root folder (/) without the necessary privileges.
mkdir /this-is-not-allowed
SUCCESS=$?
if [ $SUCCESS -eq 0 ]
then
echo "mkdir successful"
else
echo "mkdir unsuccessful" # <-- this will show after mkdir error message
fi
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: outputting script results into terminal
Thanks I don't know what I did wrong last night, but when I had a terminal open, and would launch my script through the file manager (double-clicking), it wouldn't open a new terminal and the output wouldn't show either. Copy & pasted your script, and now it all works. Ah wellPilosopong Tasyo wrote:I didn't meant to step on you. I was reading the thread last night and saw your two-script solution. I thought to myself that maintaining a complementary script wasn't necessary when it can get more efficient by doing it inline. Hence the post.xenopeek wrote:...that also kills my "two scripts" solution
Hope this clarifies things.
Re: outputting script results into terminal
HAHAHAHAHAHAHAHAHA, I had a good laugh with that. I might be a n00b, but I'm not a N00B! Yes I removed the ellipses. That and this time I tried changing it to bash, which worked... well enough. I was hoping to have the command prompt available after everything is said and done. Now when I press enter, it closes with no prompt available. BUT I can settle for that. Thank you for your help, I appreciate it.Pilosopong Tasyo wrote: Humor me for a moment, you copied and pasted everything literally?
But if you can be so nice, what exactly are lines 1, 5, and 6 doing?
Code: Select all
tty -s
SUCCESS=$?
if [ $SUCCESS -ne 0 ]
then
gnome-terminal --command=$0
exit
fi
- Pilosopong Tasyo
- Level 6
- Posts: 1432
- Joined: Mon Jun 22, 2009 3:26 am
- Location: Philippines
Re: outputting script results into terminal
In a nutshell, the entire code fragment tries to determine if the script is indeed running within a terminal session/window or not (line 1). If it is, proceed with normal execution (the whole if-then-fi statement is bypassed). Otherwise, suspend current script (parent process), open a terminal window and run self a second time (line 5) as a child process. The $0 parameter is shorthand for the complete location and name of the script (self).lvleow wrote:...what exactly are lines 1, 5, and 6 doing?
Code: Select all
tty -s SUCCESS=$? if [ $SUCCESS -ne 0 ] then gnome-terminal --command=$0 exit fi
On the second pass, line 1 will now say that the script is indeed running in a terminal window, so if-then-fi gets ignored and the remainder of the script gets executed. Once this child process is finished, it returns control to the parent process which was suspended earlier. Since the child process was able to accomplish what the parent process was unable to do on the first pass, there's no point in executing the remainder of the script. Hence line 6.
References for further reading (terminal commands):
Code: Select all
info coreutils 'tty invocation'
man gnome-terminal
Code: Select all
#!/bin/sh
# Make sure the script is running in a terminal window
tty -s
SUCCESS=$?
if [ $SUCCESS -ne 0 ]
then # the need to spawn is inevitable
gnome-terminal --command=$0
else # put the rest of your script after this line
ifconfig wlan1 up
wait
ifconfig
wait
cd /home/user/documents
ls
read -p "Press the enter key..." DUMMY
fi
# EOF
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Re: outputting script results into terminal [SOLVED]
Oh, dôh... This feature is builtin (at least for direct launching of scripts) the gnome terminal
Open gnome terminal, go to Edit -> Profile Preferences, Title and Command tab, and change "When command exits" to "Hold the terminal open". Now run your script from the file manager by double-clicking it, and choose "Run in Terminal". Now it shows you all the output, and allows you to scroll back, until you close the window.
Open gnome terminal, go to Edit -> Profile Preferences, Title and Command tab, and change "When command exits" to "Hold the terminal open". Now run your script from the file manager by double-clicking it, and choose "Run in Terminal". Now it shows you all the output, and allows you to scroll back, until you close the window.
Re: outputting script results into terminal [SOLVED]
Following is a further improvement of Pilosopong Tasyo's example. It now also passes command line arguments on restarting the script, optionally pauses before the terminal window closes, uses your system's default terminal so it works on other desktop environments, and just the 4 lines of code need to be inserted at the top of any script.
Double-clicking a script in your file manager and choosing "Run" launches a terminal that automatically pauses at the end. Choosing "Run in Terminal" will run on a normal terminal, that doesn't pause at the end but closes the window. Starting the script from inside a terminal directly doesn't pause at the end either (but you go back to the prompt). I kinda like how this works.
I know it was already marked solved, but had some more Bash skills to hone
Double-clicking a script in your file manager and choosing "Run" launches a terminal that automatically pauses at the end. Choosing "Run in Terminal" will run on a normal terminal, that doesn't pause at the end but closes the window. Starting the script from inside a terminal directly doesn't pause at the end either (but you go back to the prompt). I kinda like how this works.
I know it was already marked solved, but had some more Bash skills to hone
Code: Select all
#!/bin/bash
# if this script was not launched from a terminal, restart it from a terminal
if [[ ! -t 0 && -x /usr/bin/x-terminal-emulator ]]; then
/usr/bin/x-terminal-emulator -e "bash -c \"$0 $*; read -s -p 'Press enter to continue...'\""
exit
fi
# YOUR CODE GOES HERE #