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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-26-2007, 06:50 PM
|
#1
|
Member
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155
Rep:
|
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 06:54 PM.
|
|
|
03-26-2007, 07:31 PM
|
#2
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
Try :
Code:
ls -lh /home | sed -n "$LNCOUNT"p
--- rod
|
|
|
03-26-2007, 07:34 PM
|
#3
|
Member
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155
Original Poster
Rep:
|
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 08:34 PM.
|
|
|
03-28-2007, 06:28 PM
|
#4
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep:
|
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
|
|
|
03-28-2007, 07:16 PM
|
#5
|
Member
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155
Original Poster
Rep:
|
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 07:21 PM.
|
|
|
03-28-2007, 08:27 PM
|
#6
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep:
|
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
|
|
|
03-29-2007, 03:14 AM
|
#7
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515
|
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 03:17 AM.
|
|
|
03-30-2007, 04:55 PM
|
#8
|
Member
Registered: Jan 2005
Location: USA
Distribution: Slackware 10.2 & 11.0
Posts: 155
Original Poster
Rep:
|
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 05:43 PM.
|
|
|
All times are GMT -5. The time now is 02:35 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|