LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2006, 07:47 PM   #1
sean@responsivedata.
LQ Newbie
 
Registered: Jan 2006
Posts: 10

Rep: Reputation: 0
Bash scripts do not echo commands


I have a bash script named ztest that looks like this:
---------------
#!/bin/bash

ls -lat

---------------

On server A it works fine, and I get a directory listing.

On server B I get nothing. As though the script did not execute!

However, I modified the script:
---------------
#!/bin/bash

ls -lat $1 >zresult

---------------

When I execute the following:
#./ztest
#cat zresult

I see the directory.
And if I execute the following:
#./ztest /etc/init.d/
#cat zresult

I see the /etc/init.d/ directory.
Bottom line: The bash script is executing.
But I am not getting/seeing any output from the script.
I have tried echo commands, >stdout, etc. but no such luck!

I am ssh'd into the server using PuTTY.
But I have no idea if this is a system configuration issue? A PuTTY issue? Some other issue?

Any ideas?
Sean McCoy
sean@responsivedata.com
 
Old 01-13-2006, 07:50 PM   #2
morrolan
Member
 
Registered: Sep 2003
Location: Manchester UK
Posts: 264

Rep: Reputation: 30
So you can't even do:

ls -lat > result.txt && less result.txt

?

That should output into a text file, then display it on the screen, but only if it was created successfully.
 
Old 01-13-2006, 07:53 PM   #3
sean@responsivedata.
LQ Newbie
 
Registered: Jan 2006
Posts: 10

Original Poster
Rep: Reputation: 0
No such luck.
cat, echo or anything that has to do with display to the screen is not working.
It writes to files just fine.
If I wanted to execute the script and then go look at a file to see how it ran, that would be fine.
But I want to see the script progress as it is executing. Not afterwards.
Not sure why it is doing this...

Sean McCoy
sean@responsivedata.com
 
Old 01-16-2006, 06:36 AM   #4
ajs318
LQ Newbie
 
Registered: Apr 2004
Location: East Midlands
Distribution: Debian SID; Mandrake
Posts: 17

Rep: Reputation: 0
DOS traps

Are you sure that you're not falling into the DOS trap, and expecting the script to display itself along with its output?!

That is if I have a file like

Code:
#!/bin/sh
env
ls /proc
cat /etc/fstab
and I run it, I will get a dump of my environment variables, a directory listing for my /proc filesystem and my table of mountable filesystems, but the commands that produced those outputs will not be included in the output. This is how it is supposed to work.

MS-DOS used to include the lines of a .BAT file in its output, unless you either preceded the command with an @ sign or used "echo off" {and "@echo off" would suppress the display of "echo off"}. But MS-DOS belongs at the bottom of the dustbin of history.


Just in case I have grasped completely the wrong end of the stick, there's another possibility which is that on "server B", you really are listing an empty directory. Perhaps your environment variables are not being inherited properly {and it's picking up some directory that happens to be empty in $PWD}. Either way, try "env" and "set" on both machines {at the command line and within a script} and see what is different.
 
Old 01-16-2006, 08:09 AM   #5
sean@responsivedata.
LQ Newbie
 
Registered: Jan 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Bash scripts issue

Hi,
No I'm not expecting to see the actual lines of the script being echoed.
Just the results of each statement.

When I put the example code, per your post, into my ztest script, and executed it. Here is what I got:

[root@responsivedata1 www]# ./ztest
[root@responsivedata1 www]#

However when I issued the env command, here is what I got:

[root@responsivedata1 www]# env
HOSTNAME=responsivedata1
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
USER=root
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36i=40;33:so=00;35:bd=40;33;01:cd=40;33;01r=01;05;37;41:mi= 01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00 ;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz =00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;3 5:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
MAIL=/var/spool/mail/root
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
INPUTRC=/etc/inputrc
PWD=/var/www
LANG=en_US.UTF-8
SHLVL=1
HOME=/root
LOGNAME=root
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/bin/env

So even though the ztest script contained the command "env", nothing ~dumped~ to stdout. But executing "env" at the comand line, everything was fine.

Now for arguements sake, if I were to put the following into ztest: env >env_dump
Then, after executing ztest; there would be a file env_dump containing the environment variables.

Thanks for looking at this issue with me!

Sean McCoy
sean@responsivedata.com
 
Old 01-17-2006, 02:56 AM   #6
ajs318
LQ Newbie
 
Registered: Apr 2004
Location: East Midlands
Distribution: Debian SID; Mandrake
Posts: 17

Rep: Reputation: 0
More things to try:

Do you get the same output for "set" and "env" executed in scripts, as you do at the command line?
Does it make a difference whether you are root or an ordinary user?
What if you make the file write to STDOUT, but use a redirect or pipe on it when you run it? eg. ./foo > foo.out or ./foo |bar {|cat is as good as anything}. Do you get anything then?
Do you get the same sorts of issues with an SSH session as with a keyboard and monitor plugged right into the machine?

Something somewhere is definitely set up funny your end ..... so now it's a case of seeing what bits are different from a "working" setup and zeroing in on the problem that way.

For the record, this is what I see when I issue "set" and then "env" in a Konsole window {KDE's own xterm}:
Code:
adam@adam3:~/perl$ set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="00" [2]="16" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='3.00.16(1)-release'
COLORTERM=
COLUMNS=132
DESKTOP_SESSION=kde
DIRSTACK=()
DISPLAY=:0
DM_CONTROL=/var/run/xdmctl
EUID=1000
GROUPS=()
GS_LIB=/home/adam/.fonts
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/adam/.gtkrc-2.0:/home/adam/.kde/share/config/gtkrc-2.0
GTK_RC_FILES=/etc/gtk/gtkrc:/home/adam/.gtkrc:/home/adam/.kde/share/config/gtkrc
HISTFILE=/home/adam/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/home/adam
HOSTNAME=adam3
HOSTTYPE=x86_64
IFS=$' \t\n'
KDE_FULL_SESSION=true
KDE_MULTIHEAD=false
KONSOLE_DCOP='DCOPRef(konsole-4375,konsole)'
KONSOLE_DCOP_SESSION='DCOPRef(konsole-4375,session-1)'
LANG=en_GB
LANGUAGE=en_GB:en_US:en_GB:en
LINES=40
LOGNAME=adam
LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
MACHTYPE=x86_64-pc-linux-gnu
MAILCHECK=60
OLDPWD=/home/adam
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/home/adam/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
PIPESTATUS=([0]="0")
PPID=4375
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
PS2='> '
PS4='+ '
PWD=/home/adam/perl
SESSION_MANAGER=local/adam3:/tmp/.ICE-unix/4334
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_AGENT_PID=4287
SSH_AUTH_SOCK=/tmp/ssh-dCXvPN4223/agent.4223
TERM=xterm
UID=1000
USER=adam
WINDOWID=37748743
XCURSOR_THEME=default
XDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,sched,rsvd,method=classic
XPSERVERLIST=':64 '
_=gpl
adam@adam3:~/perl$ env
KDE_MULTIHEAD=false
SSH_AGENT_PID=4287
DM_CONTROL=/var/run/xdmctl
TERM=xterm
SHELL=/bin/bash
XDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,sched,rsvd,method=classic
GTK2_RC_FILES=/etc/gtk-2.0/gtkrc:/home/adam/.gtkrc-2.0:/home/adam/.kde/share/config/gtkrc-2.0
GTK_RC_FILES=/etc/gtk/gtkrc:/home/adam/.gtkrc:/home/adam/.kde/share/config/gtkrc
GS_LIB=/home/adam/.fonts
WINDOWID=37748743
KDE_FULL_SESSION=true
USER=adam
LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:
SSH_AUTH_SOCK=/tmp/ssh-dCXvPN4223/agent.4223
SESSION_MANAGER=local/adam3:/tmp/.ICE-unix/4334
XPSERVERLIST=:64
KONSOLE_DCOP=DCOPRef(konsole-4375,konsole)
DESKTOP_SESSION=kde
PATH=/home/adam/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
KONSOLE_DCOP_SESSION=DCOPRef(konsole-4375,session-1)
PWD=/home/adam/perl
LANG=en_GB
SHLVL=2
HOME=/home/adam
LANGUAGE=en_GB:en_US:en_GB:en
XCURSOR_THEME=default
LOGNAME=adam
DISPLAY=:0
COLORTERM=
_=/usr/bin/env
OLDPWD=/home/adam
 
Old 01-17-2006, 07:37 AM   #7
sean@responsivedata.
LQ Newbie
 
Registered: Jan 2006
Posts: 10

Original Poster
Rep: Reputation: 0
Interesting suggestions.
>Do you get the same output for "set" and "env" executed in scripts, as you do at the command line?

No, I do not! Here are the variations between the two:
When executed within the script:
BASH_LINENO=([0]="0")
BASH_SOURCE=([0]="./ztest")
HISTSIZE=1000
IFS='
'
POSIXLY_CORRECT=y
PPID=26545
SHELLOPTS=braceexpand:hashall:interactive-commentsosix
SHLVL=2
_=/bin/sh

When executed at the command line:
BASH_LINENO=()
BASH_SOURCE=()
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=157
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
IFS=$' \t\n'
LINES=71
MAILCHECK=60
OLDPWD=/root
PIPESTATUS=([0]="0" [1]="0")
PPID=26544
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}\007"'
PS1='[\u@\h \W]\$ '
PS2='> '
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SUPPORTED=en_US.UTF-8:en_US:en
_=

>Does it make a difference whether you are root or an ordinary user?

No difference.

>What if you make the file write to STDOUT, but use a redirect or pipe on it when you run it? eg. ./foo > foo.out or ./foo |bar {|cat is as good as anything}. Do you get anything then?

Adding the pipe to cat worked....sort of. See the variation below.

[root@responsivedata1 www]# ls -lat
total 228
drwxr-xr-x 10 root root 4096 Jan 17 06:26 .
-rw-r--r-- 1 root root 903 Jan 17 06:26 zresult
-rwxr-xr-x 1 root root 19 Jan 17 06:23 ztest
-rw-r--r-- 1 root root 67 Jan 13 18:08 stdout
-rwxr-xr-x 1 root root 60 Jan 13 17:38 untar.sh
-rw-r--r-- 1 root root 100620 Jan 13 17:29 intranet.tar.gz
drwxr-xr-x 10 apache apache 4096 Jan 12 11:02 intranet
drwxr-xr-x 2 apache apache 4096 Jan 3 16:06 responsivedata
drwxr-xr-x 14 root root 12288 Jan 2 11:43 manual
drwxr-xr-x 3 root root 4096 Jan 2 11:42 icons
drwxr-xr-x 3 root root 4096 Jan 2 11:42 error
drwxr-xr-x 2 webalizer root 4096 Jan 1 04:02 usage
drwxr-xr-x 25 root root 4096 Aug 23 18:03 ..
drwxr-xr-x 2 root root 4096 May 23 2005 cgi-bin
drwxr-xr-x 2 root root 4096 May 23 2005 html
[root@responsivedata1 www]# ./ztest | cat
total 220
drwxr-xr-x 10 root root 4096 Jan 17 06:26 .
-rw-r--r-- 1 root root 903 Jan 17 06:26 zresult
-rwxr-xr-x 1 root root 19 Jan 17 06:23 ztest
-rw-r--r-- 1 root root 67 Jan 13 18:08 stdout
-rwxr-xr-x 1 root root 60 Jan 13 17:38 untar.sh
-rw-r--r-- 1 root root 100620 Jan 13 17:29 intranet.tar.gz
drwxr-xr-x 10 apache apache 4096 Jan 12 11:02 intranet
drwxr-xr-x 2 apache apache 4096 Jan 3 16:06 responsivedata
drwxr-xr-x 14 root root 12288 Jan 2 11:43 manual
drwxr-xr-x 3 root root 4096 Jan 2 11:42 icons
drwxr-xr-x 3 root root 4096 Jan 2 11:42 error
drwxr-xr-x 2 webalizer root 4096 Jan 1 04:02 usage
drwxr-xr-x 2 root root 4096 May 23 2005 cgi-bin
drwxr-xr-x 2 root root 4096 May 23 2005 html
?--------- ? ? ? ? ? ..
[root@responsivedata1 www]#

Not sure why I am getting the line with '?'

>Do you get the same sorts of issues with an SSH session as with a keyboard and monitor plugged right into the machine?

No difference.

>Something somewhere is definitely set up funny your end

Agreed.
And I really appreciate the suggestions you are giving me.
My attention is drawn to the variations between the set values for:
SHELLOPTS
SHLVL

I'll do some research as to what these are controlling.
But the question is how do I modify their settings?

Did I miss these in the /root/.bashrc file?

Thanks,
Sean McCoy
sean@responsivedata.com
 
Old 01-17-2006, 09:08 AM   #8
ajs318
LQ Newbie
 
Registered: Apr 2004
Location: East Midlands
Distribution: Debian SID; Mandrake
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by sean@responsivedata.
My attention is drawn to the variations between the set values for:
SHELLOPTS
SHLVL
SHLVL you needn't worry about much; it stands for "shell level" and increases by one everytime you start a shell within a shell. To prove it to yourself, try
Code:
$ echo $SHLVL
$ bash
$ echo $SHLVL
$ bash
$ echo $SHLVL
  (ctrl+D)
$ echo $SHLVL
  (ctrl+D)
$ echo $SHLVL
and see how it changes.
Quote:
Originally Posted by sean@responsivedata.
Did I miss these in the /root/.bashrc file?

Thanks,
Sean McCoy
sean@responsivedata.com
You don't set SHELLOPTS directly, but by means of the set command. And you've already said the problem is there with both root and non-root users, so it most probably isn't something to do with root's own .bashrc, but with the global bashrc found under /etc/. Unfortunately, different distros {you didn't say which you were using, BTW} have different ideas as to what this should be called; so have a read of the manpage to find out where your global options are set.

One more thing: it might be worth creating a special user account just for poking about with shell options {users' rc files override global ones .....} Then, if you really foul things up {like getting keypresses not to display at all and the backspace key not to work properly}, you can always login as root, kill off that user's processes and restore their rc file to something sane.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripts do not echo commands sean@responsivedata. Linux - Newbie 9 01-17-2006 10:25 AM
Bash scripts do not echo commands sean@responsivedata. Linux - Software 3 01-13-2006 09:03 PM
can echo be used for popups in shell scripts run by cron? dr_zayus69 Linux - Software 4 04-12-2005 06:30 PM
Issuing Telnet commands via bash scripts Kaj Programming 2 01-02-2005 11:44 PM
How to echo variables and commands in one line? hindll01 Programming 1 09-10-2004 06:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration