LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Pass a variable as a line number in sed (http://www.linuxquestions.org/questions/programming-9/pass-a-variable-as-a-line-number-in-sed-540847/)

dx0r515t 03-26-2007 07:50 PM

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.

theNbomr 03-26-2007 08:31 PM

Try :
Code:

        ls -lh /home | sed -n "$LNCOUNT"p
--- rod

dx0r515t 03-26-2007 08:34 PM

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.

ta0kira 03-28-2007 07:28 PM

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

dx0r515t 03-28-2007 08:16 PM

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?

ta0kira 03-28-2007 09:27 PM

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

bigearsbilly 03-29-2007 04:14 AM

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


dx0r515t 03-30-2007 05:55 PM

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.


All times are GMT -5. The time now is 08:40 PM.