[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 832
  • Last Modified:

How to read data from a file to a variable..

Trying to compare the number of files in a directory and make sure that it is increasing over time
I output the result of a ls - l piped to a wc -l
Trying to read in a value (first line) from a file and assign a to a variable... the following is the script.. where am I going wrong ??

#!/usr/bin/sh
# Script to count number of lines from the ls - l command

# Start by copying the old countfile to a new one that will be referenced later
cp /usr/byr/countfile /usr/byr/countfile2

# then pipe the output of the counting of the lines to the countfile
ls -l /usr/byr/ | wc -l > /usr/byr/countfile

# assign the variable $new to the current number of lines
# and assign $old to the previous number of lines
cut -c 1-8 /usr/byr/countfile > $new
cut -c 1-8 /usr/byr/countfile2 > $old

echo $new
echo $old

# then compare the value of the old number to the new number
# and write to the testfile a string that will be looked at by NIGHTWATCH
if [ exp $new -gt exp $old ]; then
        echo ok > /usr/byr/testfile
echo $new
echo $old
echo "ok"
elif [ exp $new -eq exp $old ]; then
        echo same > /usr/byr/testfile
echo $new
echo $old
echo "same"
else
        echo fail > /usr/byr/testfile
echo $new
echo $old
echo "fail"
fi
~
0
911bob
Asked:
911bob
  • 13
  • 9
  • 3
  • +1
3 Solutions
 
griesshCommented:
Hi 911bob,

Without knowing what your problem is I would improve the script this way:

#!/usr/bin/sh
# Script to count number of lines from the ls - l command

# Start by copying the old countfile to a new one that will be referenced later
mv /usr/byr/countfile /usr/byr/countfile2

# then pipe the output of the counting of the lines to the countfile
ls -l /usr/byr/ | wc -l | read new
echo $new > /usr/byr/countfile

# assign the variable $new to the current number of lines
# and assign $old to the previous number of lines
cat /usr/byr/countfile2 | read old

echo $new
echo $old

# then compare the value of the old number to the new number
# and write to the testfile a string that will be looked at by NIGHTWATCH
if [ $new -gt $old ]; then
        echo ok > /usr/byr/testfile
echo $new
echo $old
echo "ok"
elif [ $new -eq $old ]; then
        echo same > /usr/byr/testfile
echo $new
echo $old
echo "same"
else
        echo fail > /usr/byr/testfile
echo $new
echo $old
echo "fail"
fi


======
Werner
0
 
911bobCTOAuthor Commented:
Tried your mods, but now there is nothing getting written to the countfile.. it is an empty file... new and old are blank lines...
0
 
griesshCommented:
What is the output of the first

echo $new
echo $old

Does the "read new" part work?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
griesshCommented:
The problem with your:

cut -c 1-8 /usr/byr/countfile > $new

is that this does not assign a value to a variable. It will write the output of the 'cut' command to a file with the name of the string in "new"!
So if "new=xxx"
then the 'cut' output will be written to file 'xxx'.
0
 
911bobCTOAuthor Commented:
to Greissh.. each echo was a blank line.. there were no values displayed.

how to I get the value I have stored (string) back and assigned to the varriable $new or $old ??
0
 
griesshCommented:
What UNIX are you running on? What is your login shell?

If you do a 'read' from the command line and then a 'echo $REPLY', does that work?
0
 
911bobCTOAuthor Commented:
SUN OS 5.8 I'm not sure what shell, it is the default...the #!/usr/bin/sh  .. at the top seems to work and the script gets processed.

I'm not sure of you read from command line...

If I do a

read countfile

it comes back at a blank line. If I press Enter it comes back to the # prompt. If I then type in echo $REPLY, nothing happens..
0
 
911bobCTOAuthor Commented:
I did fix part of the problem by declaring where the first countfile should be placed, the read old.. read new... are still coming up blank. htat is the real issue.. how to get the one line from the file into a variable...
Current script is:

#!/usr/bin/sh
# Script to count number of lines from the ls - l command

# Start by copying the old countfile to a new one that will be referenced later
mv /usr/byr/countfile /usr/byr/countfile2

# then pipe the output of the counting of the lines to the countfile
ls -l /usr/byr/ | wc -l > /usr/byr/countfile


# assign the variable $new to the current number of lines
# and assign $old to the previous number of lines
cat /usr/byr/countfile2 | read old
cat /usr/byr/countfile | read new

echo $new
echo $old

# then compare the value of the old number to the new number
# and write to the testfile a string that will be looked at by NIGHTWATCH
if [ exp $new -gt exp $old ]; then
        echo ok > /usr/byr/testfile
echo $new
echo $old
echo "ok"
elif [ exp $new -eq exp $old ]; then
        echo same > /usr/byr/testfile
echo $new
echo $old
echo "same"
else
        echo fail > /usr/byr/testfile
echo $new
echo $old
echo "fail"
fi
0
 
griesshCommented:
Ok, let's try the read again:

run

read

from the command line. You will see an empty line, because the 'read' command is waiting for input. Type

'126' <Enter>

then run

echo $REPLY

You should see the '126' as output. (Do a 'man read' to learn more about the command)

If that works, then do

read line

This will read everything you type until you press the Enter key into the  variable 'line'.

echo $line

should show you all your input.


Start a script with:
#!/usr/bin/sh

ls -l /usr/byr/ | wc -l
ls -l /usr/byr/ | wc -l > /usr/byr/countfile

Do you get a number from the first command?
When you do

cat /usr/byr/countfile

is the same number in that file?
0
 
911bobCTOAuthor Commented:
First part...
Sun Microsystems Inc.   SunOS 5.8       Generic February 2000
# read
read: missing arguments
# read test
126
# echo $REPLY

# read line
this will read everything
# echo nline
nline
# echo $line
this will read everything
#

 Results of script is as you list the output is the same as the cat listing
0
 
griesshCommented:
Some more on 'read':
'read' without argument is supposed to read into the variable REPLY. It looks like your version is too old and always needs an argument.
'read line' will read all your input into the variable 'line'.
'echo nline' will just print the string 'nline' to standard out, 'echo $line' will print the CONTENT of 'line' to the output device.

That shows that the 'read' should work.

Next scipt:

#!/usr/bin/sh

#This will show the number of files
ls -l /usr/byr/ | wc -l
#This will read the number of files into the variable 'line'
ls -l /usr/byr/ | wc -l | read line
#This will show the CONTENT of the variable 'line'
echo $line


should produce the same results. What we do in the first line is to pipe the output of the 'wc' command to the 'read' command which will store that number in the 'line' variable. Please make sure that you understand that 'line' and '$line' are different things. ('$line' is the CONTENT of 'line'!)



0
 
911bobCTOAuthor Commented:
what you have given so far works fine... what next to get the variable from the new counted list to the countfile and the countfile2 value back back into the old variable.
0
 
TintinCommented:
Here's how I would write/simplify it:

#!/usr/bin/sh
# Script to count number of lines from the ls - l command

countfile=/usr/byr/countfile
testfile=/usr/byr/testfile

count=`ls /usr/byr/ | wc -l`
previous=`grep -c . $countfile`
echo $count >$countfile

if [ $count -gt $previous ]
then
   echo ok >$testfile
elif [ $count -eq $previous ]
then
   echo same >$testfile
else
   echo fail >$testfile
fi


0
 
911bobCTOAuthor Commented:
Modified the script as listed below and notice that it die not write out the number of lines in the ls -l command.... very odd.

#!/usr/bin/sh
# Script to count number of lines from the ls - l command

# Start by copying the old countfile to a new one that will be referenced later
mv /usr/byr/countfile /usr/byr/countfile2

# then pipe the output of the counting of the lines to the variable new

ls -l /usr/byr/ | wc -l | read new
echo $new
echo "should be new on line above"

# ls -l /usr/byr/ | wc -l > /usr/byr/countfile

# assign the variable $new to the current number of lines
# and assign $old to the previous number of lines
# cat /usr/byr/countfile2 | read old
# cat /usr/byr/countfile | read new

echo $new
echo $old

# then compare the value of the old number to the new number
# and write to the testfile a string that will be looked at by NIGHTWATCH
if [ exp $new -gt exp $old ]; then
        echo ok > /usr/byr/testfile
echo $new
echo $old
echo "ok"
elif [ exp $new -eq exp $old ]; then
        echo same > /usr/byr/testfile
echo $new
echo $old
"countfile.sh" 40 lines, 1002 characters
# countfile.sh
                                                                                      <<===============================Notice this value is missing....
should be new on line above




same
#
0
 
911bobCTOAuthor Commented:
Tintin did I miss something ?? Here is the script
#!/usr/bin/sh

# Script to count the number of lines from a ls - l command

countfile= /usr/byr/countfile
testfile= /usr/byr/testfile

count= 'ls /usr/byr/ | wc -l'
previous= 'grep -c.#countfile'
#!/usr/bin/sh

# Script to count the number of lines from a ls - l command

countfile=/usr/byr/countfile
testfile=/usr/byr/testfile

count='ls /usr/byr/ | wc -l'
previous='grep -c.#countfile'
echo $count >$countfile

if [ $count -gt $previous ]
then
        echo ok>$testfile
elif [ $count -eq $previous ]
then
        echo same >$testfile
else
        echo fail >$testfile
fi


Here is the output ?? error

# test.sh
/usr/bin/test.sh: test: unknown operator /usr/byr/

Any ideas ??
0
 
TintinCommented:
Sorry, a mistake in the logic.

Here is the corrected script (which I've tested on my Solaris server).

#!/usr/bin/sh
# Script to count number of lines from the ls - l command

countfile=/usr/byr/countfile
testfile=/usr/byr/testfile

count=`ls /usr/byr | wc -l`

if [ -f $countfile ]
then
   previous=`cat $countfile`
else
   previous=0
fi

echo $count >$countfile

if [ $count -gt $previous ]
then
   echo ok >$testfile
elif [ $count -eq $previous ]
then
   echo same >$testfile
else
   echo fail >$testfile
fi
0
 
911bobCTOAuthor Commented:
Where id I go wrong ?? ... see error at end

#!/usr/bin/sh

# Script to count the number of lines from a ls - l command

countfile=/usr/byr/countfile
testfile=/usr/byr/testfile

count='ls /usr/byr | wc -l'
if [ -f $countfile ]
then
        previous='cat $countfile'
else
        previous = 0
fi

if [ $count -gt $previous ]
then
        echo ok>$testfile
elif [ $count -eq $previous ]
then
        echo same >$testfile
else
        echo fail >$testfile
fi

running results in:
# test.sh
/usr/bin/test.sh: test: unknown operator /usr/byr
#
0
 
TintinCommented:
Works fine for me.

Run the script with

sh -x test.sh

and post the output.
0
 
911bobCTOAuthor Commented:
Output:

# cd /usr/bin
# sh -x test.sh
countfile=/usr/byr/countfile
testfile=/usr/byr/testfile
count=ls /usr/byr | wc -l
+ [ -f /usr/byr/countfile ]
previous=cat $countfile
+ [ ls /usr/byr | wc -l -gt cat $countfile ]
test.sh: test: unknown operator /usr/byr
#
0
 
griesshCommented:
911bob

Unfortunatelly we are working now on two different paths ...

From your last reply I assume that the 'read' worked ...

#!/usr/bin/sh

#This will show the number of files
ls -l /usr/byr/ | wc -l
#This will read the number of files into the variable 'line'
ls -l /usr/byr/ | wc -l | read line1
#This will write the number into a file
ls -l /usr/byr/ | wc -l > countfile
#and read it back in
cat countfile | read line2
#This will show the CONTENT of the variable 'line1' and 'line2'
echo line1="$line1" line2="$line2"

This should show something like
line1=67 line2=67

Let me know what you see ...
0
 
GnsCommented:
CC Werner, definetely two separate scripts, and two separate problems:-).

Your problem with Tintins perfectly fine scriptlet is the following line:
count='ls /usr/byr | wc -l'
Since you use the wrong kind of quotes you set cout to the string "ls /usr/byr | wc -l" instead of executing those commands and setting it to the result of them.
Note the difference between single quotes ' string ' and back-ticks ` execute commands ` ... So just change that line to
count=`ls /usr/byr | wc -l`
and that script would be fine.

Are we seeing verbatim quotes for the read problem you have with Werners script-variant?

-- Glenn
0
 
911bobCTOAuthor Commented:
I know you and Tintin are  going down two paths.. But I will continue to work with both.. I have 2 test files so i will put yours into one and his into the other. I really appreciate your help.

Script:
ls -l /usr/byr/ | wc -l
ls -l /usr/byr/ | wc -l | read new
ls -l /usr/byr/ | wc -l > countfile

cat countfile | read line2
echo line1="$new" line2="$line2"
 output:
# countfile.sh
      25
line1= line2=

Result of cat countfile from command prompt:

# cat countfile
      25

Very odd it does not seem to wnat to read the variable...
0
 
GnsCommented:
Hm, I think the "uncooperativeness of the read command/shell builtin is due to "stupidity" in the /usr/bin/read command, or bourne shell builtin. Switch to korn shell, and Werners script will work too... Do this by changing the
#!/usr/bin/sh
line to
#!/usr/bin/ksh

-- Glenn
0
 
griesshCommented:
We dumped our last SunOS machines a year ago, so I can't even check ... but Glenn is right, the 'read' is misbehaving as we see.
Try Glenn's suggestion to switch to ksh if possible ... hopefully you get a better result there.
0
 
911bobCTOAuthor Commented:
Thanks to everyone... I split the points as all three were helpful in geetting the it working
0
 
griesshCommented:
Does that mean the ksh version is working?
0
 
911bobCTOAuthor Commented:
Yes the ksh took care of a most of the issues...just had to clean up a bit to customize it to my needs... Thanks again...
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 13
  • 9
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now