Solved

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

Posted on 2004-08-16
27
816 Views
Last Modified: 2010-04-21
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
Comment
Question by:911bob
  • 13
  • 9
  • 3
  • +1
27 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 11810988
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
 

Author Comment

by:911bob
ID: 11811206
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
 
LVL 11

Expert Comment

by:griessh
ID: 11811440
What is the output of the first

echo $new
echo $old

Does the "read new" part work?
0
 
LVL 11

Expert Comment

by:griessh
ID: 11811521
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
 

Author Comment

by:911bob
ID: 11811755
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
 
LVL 11

Expert Comment

by:griessh
ID: 11812665
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
 

Author Comment

by:911bob
ID: 11812892
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
 

Author Comment

by:911bob
ID: 11812980
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
 
LVL 11

Expert Comment

by:griessh
ID: 11813459
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
 

Author Comment

by:911bob
ID: 11813746
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
 
LVL 11

Expert Comment

by:griessh
ID: 11814943
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
 

Author Comment

by:911bob
ID: 11815053
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
 
LVL 48

Expert Comment

by:Tintin
ID: 11815288
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:911bob
ID: 11815358
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
 

Author Comment

by:911bob
ID: 11815503
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
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 80 total points
ID: 11816768
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
 

Author Comment

by:911bob
ID: 11816928
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
 
LVL 48

Expert Comment

by:Tintin
ID: 11817049
Works fine for me.

Run the script with

sh -x test.sh

and post the output.
0
 

Author Comment

by:911bob
ID: 11818793
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
 
LVL 11

Accepted Solution

by:
griessh earned 90 total points
ID: 11820688
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
 
LVL 20

Expert Comment

by:Gns
ID: 11820938
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
 

Author Comment

by:911bob
ID: 11820979
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
 
LVL 20

Assisted Solution

by:Gns
Gns earned 80 total points
ID: 11821218
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
 
LVL 11

Expert Comment

by:griessh
ID: 11821561
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
 

Author Comment

by:911bob
ID: 11822823
Thanks to everyone... I split the points as all three were helpful in geetting the it working
0
 
LVL 11

Expert Comment

by:griessh
ID: 11824381
Does that mean the ksh version is working?
0
 

Author Comment

by:911bob
ID: 11825024
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now