outputting script results into terminal [SOLVED]

Forum rules
Before you post please read this

outputting script results into terminal [SOLVED]

Postby lvleow on Tue Sep 06, 2011 3:12 am

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?
Last edited by lvleow on Wed Sep 07, 2011 2:43 am, edited 1 time in total.
lvleow
Level 1
Level 1
 
Posts: 42
Joined: Sat Nov 06, 2010 5:32 pm

Linux Mint is funded by ads and donations.
 

Re: outputting script results into terminal

Postby xenopeek on Tue Sep 06, 2011 7:27 am

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:
Code: Select all
read -p "Press enter to continue..." -s

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:
Code: Select all
exec > your_logfile 2>&1

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
Code: Select all
#!/bin/bash
TERM=linux
export TERM
gnome-terminal --command /home/user/justdoit.sh &

Second file: /home/user/justdoit.sh
Code: Select all
#!/bin/bash
ifconfig wlan1 up
wait
ifconfig
wait
cd /home/user/documents
ls
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14510
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: outputting script results into terminal

Postby Habitual on Tue Sep 06, 2011 10:45 am

Code: Select all
#!/bin/sh
set -x
...


run the script manually, set -x will show you a more verbose output, every step of the way.
What's a landing but a take off in reverse?
My DorkBlog
User avatar
Habitual
Level 7
Level 7
 
Posts: 1999
Joined: Sun Nov 21, 2010 8:31 pm
Location: Slackware

Re: outputting script results into terminal

Postby Pilosopong Tasyo on Tue Sep 06, 2011 11:45 am

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
...
...
Image
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].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: outputting script results into terminal

Postby xenopeek on Tue Sep 06, 2011 12:08 pm

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
...
...

Cool, tried it :wink: 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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14510
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: outputting script results into terminal

Postby Pilosopong Tasyo on Tue Sep 06, 2011 6:47 pm

Vincent Vermeulen wrote:...gnome-terminal doesn't work if you already have another terminal open...Also, it doesn't pause after doing the commands.

I don't understand what you meant, but just for sh!ts and giggles I wrote a short script following the above pattern:

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 works as intended while in the GUI, opening a new terminal window and outputting the ls command as well as the prompt, waiting for me to press the enter key.

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

won't work is because the -s parameter is a bash-specific extension and it isn't recognized in Bourne shell (or dash, technically). In order for it to work, the shebang line should be changed from

Code: Select all
#!/bin/sh

to

Code: Select all
#!/bin/bash

At least as far as I remember and have experienced firsthand. Also doesn't the read command require a variable (even if it's a dummy one) at the end?

Vincent Vermeulen wrote:...that also kills my "two scripts" solution :(

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.

Hope this clarifies things. :D
Image
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].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: outputting script results into terminal

Postby lvleow on Tue Sep 06, 2011 11:10 pm

Vincent Vermeulen 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


It doesn't show me the output prior to adding the line, still same result after adding that line. just a ~ $ command prompt.

tried with both #!/bin/sh and #!/bin/bash as Pilosopong Tasyo wrote.


Habitual wrote:
Code: Select all
#!/bin/sh
set -x
...


run the script manually, set -x will show you a more verbose output, every step of the way.


set -x resulted in just a ~ $ command prompt as well

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.


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 have no idea what you did there, but I copied and pasted as you said with the same result as all the others.

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.
lvleow
Level 1
Level 1
 
Posts: 42
Joined: Sat Nov 06, 2010 5:32 pm

Re: outputting script results into terminal

Postby Pilosopong Tasyo on Wed Sep 07, 2011 1:03 am

I didn't think that it would matter, but in case it does... 32bit mint10 gnome?

I use Mint 9 (LTS) Gnome.

I have no idea what you did there, but I copied and pasted as you said with the same result as all the others.

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.

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

Save the above script in a file and enable the necessary permissions. Run the script inside a terminal window. You should see some output. Run again the script from Nautilus. It should open a terminal window and you should see the same output. If the read command doesn't work, change the shebang line from #!/bin/sh to #!/bin/bash and try again. If you still don't see any kind of output then I do not know why. Probably it's the difference between Mint 9 and 10, but I can't fathom that is the bottom-line reason.

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 ~ $

(2) Commands also generate what is called an exit code. If the command runs successfully, it exits passing the value 0 (zero) to a special variable $?. Non-zero values usually means -- you've guessed it -- errors or some other issue. As always, read up the man page for the particular command to find what exit code it generates. The following code fragment demonstrates how it's used:

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


HTH.
Image
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].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: outputting script results into terminal

Postby xenopeek on Wed Sep 07, 2011 2:27 am

Pilosopong Tasyo wrote:
Vincent Vermeulen wrote:...that also kills my "two scripts" solution :(

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.

Hope this clarifies things. :D

Thanks :D 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 well :wink:
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14510
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: outputting script results into terminal

Postby lvleow on Wed Sep 07, 2011 2:42 am

Pilosopong Tasyo wrote:Humor me for a moment, you copied and pasted everything literally?


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.

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


Once again, thank you. :)
lvleow
Level 1
Level 1
 
Posts: 42
Joined: Sat Nov 06, 2010 5:32 pm

Re: outputting script results into terminal

Postby Pilosopong Tasyo on Wed Sep 07, 2011 5:00 am

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

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).

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

Supplementary: Another alternative is to use the full if-then-else-fi statement. No need for an exit command:

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
Image
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].
User avatar
Pilosopong Tasyo
Level 6
Level 6
 
Posts: 1101
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: outputting script results into terminal [SOLVED]

Postby xenopeek on Wed Sep 07, 2011 12:55 pm

Oh, dôh... This feature is builtin (at least for direct launching of scripts) the gnome terminal :D

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14510
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: outputting script results into terminal [SOLVED]

Postby xenopeek on Thu Sep 08, 2011 4:28 pm

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 :wink:

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 #
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14510
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 4 guests