LinuxQuestions.org
Visit Jeremy's Blog.
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 03-06-2007, 03:31 PM   #1
LNewbie
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Rep: Reputation: 0
Newbie Bash Program Problem with Redirection


Hello,

I am new to this forum and I am working on a lab assignment that supplies the program and then asks for me to run the program from command line, then use at command, diff and cron.

The problem that I am having is that I can't get program to run correctly. It doesn't redirect the stdout to the file. I know that I must have a syntax error. I am running Fedora Core 3.

Any hints or suggestions would be appreciated.

Thank you.

This is the first few lines of the program.

#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE
 
Old 03-06-2007, 03:39 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by LNewbie
Hello,

I am new to this forum and I am working on a lab assignment that supplies the program and then asks for me to run the program from command line, then use at command, diff and cron.

The problem that I am having is that I can't get program to run correctly. It doesn't redirect the stdout to the file. I know that I must have a syntax error. I am running Fedora Core 3.

Any hints or suggestions would be appreciated.

Thank you.

This is the first few lines of the program.

#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE
There's nothing wrong with that portion of your script. Are you sure you can write to /tmp?
 
Old 03-06-2007, 07:40 PM   #3
LNewbie
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Re: Newbie Bash Program Problem with Redirection

Thank you for the reply.

I assumed that I could write to /tmp because I am logged in as root. I am using root to run the chron jobs after I get this program running.

If I remove the $ just before OUTFILE, it creates a file named OUTFILE with the correct output.

----------


#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE
 
Old 03-06-2007, 09:52 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by LNewbie
Thank you for the reply.

I assumed that I could write to /tmp because I am logged in as root. I am using root to run the chron jobs after I get this program running.
Don't login as root. Use sudo, or su if you really need to. Never run anything as root that can be done by an ordinary user, especially writing and testing scripts.

Quote:
If I remove the $ just before OUTFILE, it creates a file named OUTFILE with the correct output.
What system are you using, and which shell (/bin/sh can be any one of several shells)?

Quote:
#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE
Did you check the value of $OUTFILE? Add a line to echo it, so that you can read its value.
 
Old 03-06-2007, 11:27 PM   #5
LNewbie
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Don't login as root. Use sudo, or su if you really need to. Never run anything as root that can be done by an ordinary user, especially writing and testing scripts.
I am using root because that is the instruction for this lab. I am using a standalone home test pc for the lab. Once I get the program working, I have to run it from command line, then use the at command and compare the files using diff. Then run it from cron as root.

Quote:
What system are you using, and which shell (/bin/sh can be any one of several shells)?
The system is a standalone test home pc running Fedora Core 3 2.6.9-1.667 and bash shell.

Quote:
Did you check the value of $OUTFILE? Add a line to echo it, so that you can read its value.
I will try this.

Thank you
 
Old 03-07-2007, 12:24 AM   #6
slantoflight
Member
 
Registered: Aug 2005
Distribution: Smoothwall
Posts: 283
Blog Entries: 3

Rep: Reputation: 35
Code:
#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE
Once you launch a program, your bash variables become pretty irrelevant outside the script.anything you do environmentally is lost once you leave #/bin/sh

in fact you'll notice anything after exec doesn't even run..
unless you fork it.. like this exec >$OUTFILE &&

but you're still back to square one.

So the solution.

remove exec and replace it with the name of your program.

Last edited by slantoflight; 03-07-2007 at 12:40 AM.
 
Old 03-07-2007, 10:22 AM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by slantoflight
Code:
#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE
Once you launch a program, your bash variables become pretty irrelevant outside the script.anything you do environmentally is lost once you leave #/bin/sh

in fact you'll notice anything after exec doesn't even run..
unless you fork it.. like this exec >$OUTFILE &&
That is not correct.

1. The line, "exec >$OUTFILE", redirects the script's stdout to $OUTFILE. The script then continues.

2. You do not use && to put a command into the background; you use &.
 
Old 03-07-2007, 11:18 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The rest of the program could be the problem. If output is redirected elsewhere in the script, $OUTFILE will be empty (but it will be created!).

I.e:
Code:
#!/bin/sh
OUTFILE="/tmp/lab6.$$"
exec >$OUTFILE

ls -l 1>/tmp/NOT.lab6.$$ 2>&1
If you run this, /tmp/lab6.<pid> will be created, but empty. /tmp/NOT.lab6.<pid> will hold all the output.

If /tmp/lab6.<pid> isn't even created, the problem lies elsewhere.

Hope this helps.
 
Old 03-08-2007, 05:51 AM   #9
LNewbie
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you for the help. This is the program supplied by the professor but I can't get it working.

The next part that I am marked on asks for me to run the program from the command line then use the AT command and compare using diff. Then run the command from cron using root. Run the command using a different user. Try to set up a cron tab with error. Then try to submit the job to run in 2029. Then clean-up and remove your jobs.

I thought if you knew what needs to be done after the program is working. It might help. I am not asking for help or assistance with the part that I am being marked on.



Code:
#!/bin/sh
OUTFILE="/tmp/lab6.$$"
# This command redirects stdout to the file
exec >$OUTFILE
# These are back quotes around 'date' in the line below
echo "running at `date`"
echo -n "My parent is PID $PPID, "
# use echo "My parent is PID $PPID,\c" on a System V system
ps ax | grep $PPID | grep -v grep | cut -c 27-
# You'll need to modify the above line if you're on a system
# with an AT & T ps. You may also need to change the column in
# the cut command if you're using anything but Redhat 6.0
echo "=============="
echo "My environment"
echo "============="
env
 
Old 03-08-2007, 06:55 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Could you be a bit clearer about what works and what doesn't?
- Do you get any error messages when running the script?
- Is the outfile (lab6.<pid>) created if you run the script?
- If so: Is there anything in it?

In general about the script you work with:

First thing I would change is this: #!/bin/sh => #!/bin/bash
On a linux box it will probably make no difference (most of the time sh is linked to bash), but on a unix box it will make a big difference (sh is not the same as bash). PPID for example is not a known variable in sh, it is in bash (on a unix box that is).

ps ax is also not known on all linux/unix flavors (as stated in the script itself), you probably need to change this to ps -ef (or alike) and change the cut statement accordingly.

After some minor changes (sh->bash and the ps ax line), the script does work on a SUN box running SunOS 5.9.

Hope this gets you going again.
 
Old 03-09-2007, 12:12 AM   #11
LNewbie
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Hi,

Quote:
Could you be a bit clearer about what works and what doesn't?
- Do you get any error messages when running the script?
No, error message

Quote:
- Is the outfile (lab6.<pid>) created if you run the script?
- If so: Is there anything in it?
No, the lab6.<pid> file doesn't get created. That is the part that doesn't work. I kept changing and testing the script and at one point, the files were created correctly. I deleted them and cleaned up. I went to run the lab again from the start and it doesn't create the files anymore.

I made the suggested changes
Quote:
#!/bin/bash and changed ps ax
and the outfile (lab6.<pid>) didn't get created

Thanks
 
Old 03-09-2007, 03:01 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi again,

A few more questions, since we still haven't found the problem:

- What are the permissions of the script (post a ls -l <scriptname>)?
- How do you execute the script?
- Could you post the output of the following script:
Code:
#!/bin/sh
set -x
OUTFILE="/tmp/lab6.$$"
echo $OUTFILE
exec >$OUTFILE
ls -l /tmp/
Lets hope this gets us a clue.
 
Old 03-09-2007, 01:46 PM   #13
hoodedmanwithsythe
Member
 
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91

Rep: Reputation: 15
the problem is this
Code:
exec >$OUTFILE
put the whole script(excluding the env variable assignment for $outfile) in a function then you can do this instead
Code:
function1 > $outfile
which is a much better way of doing things
like this
for example
Code:
#!/bin/sh
OUTFILE="/home/timothy/lab6.txt"


function1()
{


# These are back quotes around 'date' in the line below
echo "running at `date`"
echo -n "My parent is PID $PPID, "
# use echo "My parent is PID $PPID,\c" on a System V system
ps ax | grep $PPID | grep -v grep | cut -c 27-
# You'll need to modify the above line if you're on a system
# with an AT & T ps. You may also need to change the column in
# the cut command if you're using anything but Redhat 6.0
echo "=============="
echo "My environment"
echo "============="
env 

}

function1 > $OUTFILE

cat $OUTFILE
 
Old 03-14-2007, 11:37 PM   #14
LNewbie
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you. This program worked and I finally have some output to read. I made a mistake because the output showed up in the terminal window. But, I will fix that

Quote:
put the whole script(excluding the env variable assignment for $outfile) in a function then you can do this instead. For example:

#!bin/sh
OUTFILE="/home/timothy/lab6.txt"


function1()
{


# These are back quotes around 'date' in the line below
echo "running at `date`"
echo -n "My parent is PID $PPID, "
# use echo "My parent is PID $PPID,\c" on a System V system
ps ax | grep $PPID | grep -v grep | cut -c 27-
# You'll need to modify the above line if you're on a system
# with an AT & T ps. You may also need to change the column in
# the cut command if you're using anything but Redhat 6.0
echo "=============="
echo "My environment"
echo "============="
env

}

function1 > $OUTFILE

cat $OUTFILE
 
Old 03-18-2007, 10:25 AM   #15
hoodedmanwithsythe
Member
 
Registered: Apr 2006
Location: West Midlands, UK
Distribution: mandriva, centos, debian
Posts: 91

Rep: Reputation: 15
Quote:
Originally Posted by LNewbie
Thank you. This program worked and I finally have some output to read. I made a mistake because the output showed up in the terminal window. But, I will fix that
the script I have written does both it outputs to the terminal as well as outputting to the filethats what the
Code:
cat $outfile
does it reads the file
i had changed the outfile location to suit me so here is the corrected version of your script with the original outfile location
Code:
#!bin/sh
OUTFILE="/tmp/lab6.$$"


function1()
{


# These are back quotes around 'date' in the line below
echo "running at `date`"
echo -n "My parent is PID $PPID, "
# use echo "My parent is PID $PPID,\c" on a System V system
ps ax | grep $PPID | grep -v grep | cut -c 27-
# You'll need to modify the above line if you're on a system
# with an AT & T ps. You may also need to change the column in
# the cut command if you're using anything but Redhat 6.0
echo "=============="
echo "My environment"
echo "============="
env

}

function1 > $OUTFILE

cat $OUTFILE
sorry i should have told you about that earlier rather than assuming you knew what i did there

Last edited by hoodedmanwithsythe; 03-18-2007 at 10:42 AM.
 
  


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 scripting I/O redirection error blinux1 Programming 13 03-18-2008 08:21 PM
Bash I/O redirection problem R00ts Programming 1 10-26-2006 07:50 PM
bash: socket redirection? Thinking Linux - Software 1 04-15-2006 12:20 PM
Bash redirection and subshells nx5000 Programming 2 11-03-2005 06:13 AM
Bash redirection being taken literally by programs R00ts Programming 13 05-26-2005 12:46 AM

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

All times are GMT -5. The time now is 09:49 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