LinuxQuestions.org
Review your favorite Linux distribution.
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 07-24-2016, 09:18 AM   #1
hawkeyesc72
LQ Newbie
 
Registered: Mar 2016
Posts: 12

Rep: Reputation: Disabled
Bash script question


Hello everyone!

I am working on a small script that reads the /etc/passwd file and displays certain fields of a specified user's entry. The output of the file should go to both the display AND to a file.

I have the script working but am wondering if there's a more elegant way to accomplish it.

I first created the script so that it ran successfully on the screen. No problems here. Then I tried making it a function and calling the function while piping it to tee to send it to both screen and file. This is where the issues begin. I keep getting error messages on the grep command I run in the script looking for the given user in /etc/passwd. The user is provided as an argument when the script is called at the command line.

I removed the function portions and tried running

exec | tee /pathname/filename

at the end of my script and it runs on the display and the proper filename is created but the output is not sent to the file. It shows on the display correctly but nothing is in the file.

I have Googled everything I can think of. I've tried re-writing the function again but keep getting the grep error.

The resolution I've found for now is to pipe each line of the script to tee /pathname/filename individually (with an -a after the initial pipe to tee).

But there has to be a little more elegant way to accomplish this.

Any advice/suggestions?

Thanks!
 
Old 07-24-2016, 10:22 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Try this:
Code:
# test#1: output to stdout
./yourscript "root"

# test#2: output to stdout and file
./yourscript "root" | tee "youroutput.txt"
If it still doesn't work, you should check if your script writes its output to stderr or /dev/tty -- if so, fix it

Last edited by NevemTeve; 07-24-2016 at 10:24 AM.
 
Old 07-24-2016, 01:15 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Above should help with output issue, but your grep issue will only be solved if you actually show us some code ... otherwise we have no idea why you are getting errors.
 
1 members found this post helpful.
Old 07-24-2016, 01:32 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by hawkeyesc72 View Post
Then I tried making it a function and calling the function while piping it to tee to send it to both screen and file. This is where the issues begin.
show us your "code".
[code]script or code output here[/code]

Thank you.
 
1 members found this post helpful.
Old 07-25-2016, 10:32 AM   #5
hawkeyesc72
LQ Newbie
 
Registered: Mar 2016
Posts: 12

Original Poster
Rep: Reputation: Disabled
code

Quote:
Originally Posted by Habitual View Post
show us your "code".
[code]script or code output here[/code]

Thank you.
OK this is a slimmed down version of what my program should do. It should collect fields 1 and 7 from the /etc/passwd file and then look for a specific user (grep $1).

It works well displaying on screen but the tee portion doesn't work completely because it only creates the file but it's not filled with the output. In my full file I had to put the | tee portion at the end of each line to make it work properly but am wondering if/hoping there is a more elegant way to do all of this in a single line.

Thanks!


CODE:

#!/bin/bash

argument=$1
DATE=$(date +"%m%d%y%k%M")

echo "TPS REPORT LISTING"
echo

awk -F':' '{print $1", " $7}' /etc/passwd | grep $1
echo

echo "END REPORT LISTING"


exec | tee -a ~/scripts/tps-list-$1-$DATE.rpt

OUTPUT:
Running the script like this: sh tps-gen.sh chris I get this on the display:

TPS REPORT LISTING

chris, /bin/bash

END REPORT LISTING


and doing an ls to the directory where the file needs to be created I can see the filename but an ls -l shows it with a size of 0:

ls
testFunction.sh tps-gen.sh tps-list-chris-0725161728.rpt

ls -l
total 8
-rwxrwxrwx 1 chris chris 220 lip 25 17:28 tps-gen.sh
-rw-rw-r-- 1 chris chris 0 lip 25 17:28 tps-list-chris-0725161728.rpt
 
Old 07-25-2016, 11:01 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Could you please edit your post and add [code] and [/code] tags?
 
Old 07-25-2016, 11:42 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So my question back would be, except for a couple of simple echo statements, awk does all the work (and grep is not needed) so you really could have done all this in an awk script.

So my suggestion would be to either make it an awk script, or simply use a bash while loop and read the file and make the output as required.
Assuming you stay in bash, the trick would be to group together (clue!) all the lines that create output so you can then place the tee redirection only once
 
1 members found this post helpful.
Old 07-26-2016, 12:38 AM   #8
hawkeyesc72
LQ Newbie
 
Registered: Mar 2016
Posts: 12

Original Poster
Rep: Reputation: Disabled
Smile interesting

Quote:
Originally Posted by grail View Post
So my question back would be, except for a couple of simple echo statements, awk does all the work (and grep is not needed) so you really could have done all this in an awk script.

So my suggestion would be to either make it an awk script, or simply use a bash while loop and read the file and make the output as required.
Assuming you stay in bash, the trick would be to group together (clue!) all the lines that create output so you can then place the tee redirection only once
Hmmm, interesting. I will research awk more and see what I can learn. Thanks for the hint.
 
  


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 script question theKbStockpiler Linux - Newbie 5 12-05-2013 04:34 PM
[bash-script] A question about using grep in the script thomas2004ch Linux - Software 2 03-05-2012 03:27 AM
Bash script CD question olan Linux - Newbie 6 11-06-2009 02:57 AM
Bash Script Question nade Programming 2 05-22-2009 09:36 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM

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

All times are GMT -5. The time now is 07:54 PM.

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