how do I print text to a terminal in bash?

Asked by Larry Jordan

      I have scripts I had written while working in Fedora Core 2 that USED to print output to a command terminal.. the simplest ones read the text from a file into a variable and used the 'cat' command to print them. This no longer seems to work from within a script. For one thing, the gnome-terminal comes up as an interactive terminal, but I've tried to use xterm with parameters and can't seem to get it right. Has BASH changed that much or is it a difference in Ubuntu?

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Ed S
Solved:
Last query:
Last reply:
Revision history for this message
Ed S (edgar-b-dsouza) said :
#1

Sorry, haven't understood your problem. Basically, to print the contents of a variable, I'd use:
echo $variablename

To print the contents of a file, I'd use:
cat filename

I don't think these basics have changed since even very early versions of Bash, so we'd need to see (and try out) commands that are causing you problems. Could you please provide some of the commands in your script (or the entire script, if a small one) that fail to provide output when run in a gnome-terminal? Additionally, if you have errors shown when you run the script, could you paste those as well?

Revision history for this message
Larry Jordan (larryjor) said :
#2

     Sorry for long wait getting back to this, I don't seem to get emails telling me that someone has posted a reply anymore.
     What I am trying to do is run a program (a bash script) in a terminal, so the program is supposed to bring up a routine in a terminal using something like 'xterm -e myprogram' and then the program would have listed the contents of the file created by the main routines. This worked well for me as a fancy greeting program in old style Fedora Core (whatever version of Bash that was) in that I could have the main program look through file to announce upcoming birthdays, anniversaries, etc., and it would display the results in a terminal and simultaneously call Festival to read the file aloud. Now, the terminal never starts and neither does Festival.

Revision history for this message
Ed S (edgar-b-dsouza) said :
#3

Well, I hope you see this reply sooner :-)

Since you didn't post any sample code, I decided to throw together a couple of rudimentary sample scripts myself. These work for me (see the comments block in the first 'file' below - that was my only real problem) so how about you try running these yourself? If it works on your system, then your shell script probably just needs a little debugging - or to be made executable. BTW - you haven't by any chance mounted the partition on which the script lives, with the "noexec" option to mount, have you?

For the record: I tested this under 10.04 Lucid Lynx, Bash version 4.1.5(1)-release (i486-pc-linux-gnu), xterm package version 256-1ubuntu1.

==================================
test.sh - the 'parent' script
==================================
#! /bin/bash

# Test file for LP Qn 108570

xterm -T "Running in_xterm.sh" -e "/tmp/in_xterm.sh"
# The above line works if the called shell script is executable (chmod u+x /tmp/in_xterm.sh) but doesn't
# work otherwise (xterm window is a half-second flicker on taskbar, and gone; no error message :-( )
# If the called shell script is NOT executable, the next line works:
#xterm -T "Running in_xterm.sh" -e "/bin/bash /tmp/in_xterm.sh"

echo "Called xterm and script ended."
==================================

==================================
Secondary script - in_xterm.sh
==================================
#! /bin/bash

# Test file, secondary, for LP Qn 108570

MSG="Greets, dude!"
echo $MSG
festival -b "(SayText \"$MSG\")"
sleep 5

MSG="Gotta go now..."
echo $MSG
festival -b "(SayText \"$MSG\")"

sleep 2
==================================

Let us know how that goes :-)
Ed.

Revision history for this message
Ed S (edgar-b-dsouza) said :
#4

Oops... watch out for line wrapping on the two longest lines in test.sh... wish there was a way of specifying a [code] and [/code] block here...

Revision history for this message
Larry Jordan (larryjor) said :
#5

     Tried this both ways (in_xterm.sh executable and not executable) and get a half second flicker either way. Seems as though it SHOULD be on for about 7 seconds (due to 2 sleep commands) with would be more than enough time to read the $MSG... so reproduces the basic problem effectively I guess. I've played around with my original scripts enough so I don't have true originals and never came up with anything that WOULD hang around, but can say I was a little more fancy with my version of in_xterm.sh - I had used:
-----------------------------------------------------------------
#!/bin/bash
shopt -s -o monitor # to get a job number
r=$[ $( wc -w $stupid | cut -b1-2 ) ]

....
function sayit {
festival -b --tts $MSG &
}
cat $MSG # with $MSG being saved in a file
if [ $TALK = "YES" ] ; then # variable TALK is set in main script if speech to text desired
     sayit
     wait %fes
else
     sleep $r
fi
---------------------------------------------------------------

     That being the basics of it. Of course, the problem seems reproduced all the same. I've changed it enough somehow that my terminals come up, with no text showing in them, and no messages read aloud. I save the processing messages to a file, and the file shows no error specific to say why. Seems to me, though, as I stated earlier this post, that the sleep statements should be more than sufficient to at least hold the terminal in place long enough to see whether there is a $MSG at least. So can't understand why it is just a momentary flicker...

Revision history for this message
Best Ed S (edgar-b-dsouza) said :
#6

(in test.sh)
#xterm -T "Running in_xterm.sh" -e "/bin/bash /tmp/in_xterm.sh"

Did you try uncommenting the line above, and commenting the other xterm line, so there is a real binary executable being invoked first, and the name of the shell script is just an argument...?

In my limited testing with the sample pair of scripts above, I found that the xterm half-second flicker is because it can't find any executable to run. Why it doesn't return an error status I don't know - that's annoying. Still, I hope this is what's behind your problem.

Revision history for this message
Larry Jordan (larryjor) said :
#7

Thanks Ed S, that solved my question.

Revision history for this message
Larry Jordan (larryjor) said :
#8

     Wanted to add, aside from that seems to solve the problem, a more personal thanks. Will have to get back to my old script now and fix it so it works again, but that sure looks like it fixed everything perfectly. Thanks, Ed!

Revision history for this message
Ed S (edgar-b-dsouza) said :
#9

You're welcome, Larry, glad to have been able to help, and glad I learned about this vagary of xterm in the process :-)

Cheers,
Ed.