LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-26-2007, 07:50 PM   #1
dx0r515t
Member
 
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155

Rep: Reputation: 30
Pass a variable as a line number in sed


Code:
ls -lh /home | sed -n 3p
The above command works great; it prints line three of the output from ls. However I'm writing a bash script and really need that "3" to be a variable. Anyone have any ideas how I could accomplish this? Here is what I have so far (completely unfinished):

Code:
#!/bin/bash
TOTAL=`ls -lh /home | wc -l`
COUNT=0
LNCOUNT=2

while [ "$COUNT" -ne "$TOTAL" ]; do
        ls -lh /home | sed -n $LNCOUNTp
        LNCOUNT=`expr $LNCOUNT + 1`
        COUNT=`expr $COUNT + 1`
done

echo $TOTAL
echo $COUNT
Note that in the above code on line seven sed -n $LNCOUNTp doesn't work. $LNCOUNT is instantiated as two so to me that should be the same as sed -n 2p. I'm sure this is a simple problem but I'm stumped at the moment.

Thanks in advance.

Last edited by dx0r515t; 03-26-2007 at 07:54 PM.
 
Old 03-26-2007, 08:31 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Try :
Code:
        ls -lh /home | sed -n "$LNCOUNT"p
--- rod
 
Old 03-26-2007, 08:34 PM   #3
dx0r515t
Member
 
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155

Original Poster
Rep: Reputation: 30
Thanks for the help rod thats just what I needed. For anyone thats interested here's the finished product:

Code:
#!/bin/bash
#Written by: Scott Sullivan -- 3/26/2007
#---------------------------------------
#This script outputs file permissions & disk usage statistics for user /home directories as well as the /root directory.

#If the current user is root continue execution otherwise exit.
if [ "$UID" -ne "0" ]
 then
	echo "----------ERROR!----------"
	echo "You must be root to run this script. Please login as root and try again."
	echo "--------------------------"
	exit
 fi
echo "-------------------------------------------------------------------------------------------------------- "
echo "Now viewing file permissions and disk usage statistics for user /home directories and /root directory..."
echo "-------------------------------------------------------------------------------------------------------- "

TOTAL=`ls -lh /home | wc -l`
COUNT=0 
LNCOUNT=1

#Look at disk usage and permissions in /home.
while [ "$COUNT" -ne "$TOTAL" ]; do
	ls -lh /home | sed -n "$LNCOUNT"p
	echo "_____________________________________________________________"
	du -sh /home/* | sed -n "$LNCOUNT"p
	LNCOUNT=`expr $LNCOUNT + 1` 
	COUNT=`expr $COUNT + 1` 
done
#Look at disk usage and permissions in /root. 
du -sh /root
ls -lhd /root
echo "_____________________________________________________________"

echo "All done."
Thanks again.

Last edited by dx0r515t; 03-26-2007 at 09:34 PM.
 
Old 03-28-2007, 07:28 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe you could substitute with this? No need for math or duplicated operations (such as ls-ing and du-ing everything each time around.)
Code:
unalias ls #this removes the default formatting of 'ls' if your system has it
ls -d1 /home/*/ | while read file; do
  ls -lhd "$file"
  du -sh "$file"
done
ta0kira
 
Old 03-28-2007, 08:16 PM   #5
dx0r515t
Member
 
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155

Original Poster
Rep: Reputation: 30
Thanks for the tip ta0kira, I guess you learn something new everyday. I tried implementing your solution and below is what I got. I'm not sure exactly what you meant when you said "ls -lhd "$file""; you can see my interpretation of it below.

Code:
#!/bin/bash
#Written by: Scott Sullivan -- 3/26/2007
#---------------------------------------
#This script outputs file permissions & disk usage statistics for user /home directories as well as the /root directory.

#If the current user is root continue execution otherwise exit.
if [ "$UID" -ne "0" ]
 then
        echo "----------ERROR!----------"
        echo "You must be root to run this script. Please login as root and try again."
        echo "--------------------------"
        exit
 fi
echo "-------------------------------------------------------------------------------------------------------- "
echo "Now viewing file permissions and disk usage statistics for user /home directories and /root directory..."
echo "-------------------------------------------------------------------------------------------------------- "

TOTAL=`ls -lh /home | wc -l`
COUNT=0
LNCOUNT=1

#Look at disk usage and permissions in /home.
ls -dl /home/*/ | while read file; do
        echo "_____________________________________________________________"
        ls -lhd /home/*/ | sed -n "$LNCOUNT"p
        du -sh /home/*/ | sed -n "$LNCOUNT"p
        LNCOUNT=`expr $LNCOUNT + 1`
done
#Look at disk usage and permissions in /root.
echo "_____________________________________________________________"
ls -lhd /root
du -sh /root
echo "_____________________________________________________________"

echo "All done."
Do I really need to use sed in this script?

Last edited by dx0r515t; 03-28-2007 at 08:21 PM.
 
Old 03-28-2007, 09:27 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You should be able to replace lines "TOTAL=`ls -lh /home | wc -l`" through "done" with the loop I gave you; no 'sed' or addition required. To see what I mean, paste the loop directly into a terminal, as-is, right from my post. You do need to make sure that the first call to 'ls' uses '-d1' and not '-dl' because '-l' gives you long attributes whereas '-1' forces one file name per line.

'-lhd' is for long attributes, human-readable sizes, and display a directory as such (and don't sub into it.) In order for '-d' to work the directory needs to be postfixed with '/', but that comes from the first 'ls' up at the top.
ta0kira
 
Old 03-29-2007, 04:14 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
also

if it is an error you should exit with a non-zero status, and maybe think
about sending messages to stderr.
Also I hate multiple echo lines,

try:
Code:
>&2 cat <<EOF

 ----------ERROR!----------
 You must be root to run this script. Please login as root and try again.
 --------------------------

EOF
exit 1

Last edited by bigearsbilly; 03-29-2007 at 04:17 AM.
 
Old 03-30-2007, 05:55 PM   #8
dx0r515t
Member
 
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155

Original Poster
Rep: Reputation: 30
Ok here is the third version of the script...
Code:
#!/bin/bash
#Written by: Scott Sullivan -- 3/26/2007
#---------------------------------------
#This script outputs file permissions & disk usage statistics for user /home directories as well as the /root directory.

#If the current user is root continue execution otherwise exit.
if [ "$UID" -ne "0" ]
 then
>&2 cat <<EOF
 ----------ERROR!----------
 You must be root to run this script. Please login as root and try again.
 --------------------------
EOF
exit 1
fi

2>&1 cat <<EOF
"-------------------------------------------------------------------------------------------------------- "
"Now viewing file permissions and disk usage statistics for user /home directories and /root directory..."
"-------------------------------------------------------------------------------------------------------- "
EOF

#Look at disk usage and permissions in /home.
ls -d1 /home/*/ | while read file; do
  echo "_____________________________________________________________"
  ls -lhd "$file"
  du -sh "$file"
done
#Look at disk usage and permissions in /root.
echo "_____________________________________________________________"
ls -lhd /root
du -sh /root
echo "_____________________________________________________________"

echo "All done."
Hopefully this time I have properly implemented your solutions. I will say this script seems much leaner/cleaner.
I think my next task will be to make this script send the output to a log file so I can make it run as a cron job on my server and examine the output at a later date.

Last edited by dx0r515t; 03-30-2007 at 06:43 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 08:07 AM
pass javascript variable to php ALInux Programming 6 01-06-2006 07:20 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 02:24 PM
Pass text to variable Zed Linux - Software 6 05-12-2003 04:02 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration