nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

[file.handling] problem

This is a discussion on [file.handling] problem within the Shell scripting forums, part of the Development/Scripting category; Hi! I want to write a script which reads and writes some data from files. Here it goes: Code: #!/bin/sh ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 10-04-2006, 04:52 PM
Junior Member
User
 
Join Date: Oct 2006
Posts: 5
Rep Power: 0
fahur
Default [file.handling] problem

Hi!

I want to write a script which reads and writes some data from files.

Here it goes:

Code:
#!/bin/sh

echo `gsh list_atm_vc -eq { 2 11 2 1 } > list_atm_vc.txt`

mFile="list_atm_vc.txt"

echo `wc -l list_atm_vc.txt > temp`

read num rest < "temp"

PORT=""
VPI=""
VCI=""

i="1"

while [ 1 ]
do
       if [ "$i" = "0" ]; then
               read PORT VPI VCI || break
               echo `gsh get_atm_vc -eq { 2 11 2 1 } -port $PORT -vpi $VPI -vci $VCI`
       fi
       i=`expr $i + 1`
done < $mFile

rm $mFile
rm $tFile
What it does (or what I want it to do :

Following command:

Code:
echo `gsh list_atm_vc -eq { 2 11 2 1 } > list_atm_vc.txt`
produces output like this (file: list_atm_vc.txt):

Code:
Port VPI VCI
0    0   101
0    0   100
then for each line, except the first and last (empty) I invoke a command:

Code:
echo `gsh get_atm_vc -eq { 2 11 2 1 } -port $PORT -vpi $VPI -vci $VCI`
where $PORT, $VPI and $VCI variables are being read from list_atm_vc.txt file.

To overlook first and last line form above mentioned line I check number of lines with command:

Code:
echo `wc -l list_atm_vc.txt > temp`
and write it to the file called 'temp'.

My problem is that I can't figure out, how to get this number of lines (get errors or no data in variable ) and use it in IF clause.

The output of above command is:

Code:
       4 list_atm_vc.txt
sa I have to read the first part of this output.

To sum up...

I'd like to:
1. Invoke a command which gives an output
2. Check number of lines of above output
3. Invoke another command with parameters read from lines, EXCEPT first and last line.

Thanks for any idea...

--
fahur
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-04-2006, 11:57 PM
sweta's Avatar
Contributors
User
 
Join Date: Feb 2005
Location: New Delhi
My distro: Suse, RHEL, Vista
Posts: 154
Rep Power: 4
sweta will become famous soon enough
Default

So you want to read data from list_atm_vc.txt file

BTW which Embedded Linux version/distro you are using?
__________________
Friends - v-nessa - missyAdmin - LinuxChix
Reply With Quote
  #3 (permalink)  
Old 10-05-2006, 08:48 PM
Junior Member
User
 
Join Date: Oct 2006
Posts: 5
Rep Power: 0
fahur
Default

Quote:
Originally Posted by sweta
So you want to read data from list_atm_vc.txt file
Yes... and use in IF clause...

Quote:
Originally Posted by sweta
BTW which Embedded Linux version/distro you are using?
Whis is what I got when logging in:

Code:
 Sun Microsystems Inc.   SunOS 5.8       Generic Patch   October 2001
Thanks...

--
fahur
Reply With Quote
  #4 (permalink)  
Old 10-06-2006, 02:04 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
My distro: Ubuntu
Posts: 1,036
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default Re: [file.handling] problem

Quote:
Originally Posted by fahur
Invoke another command with parameters read from lines, EXCEPT first and last line.fahur
Following command will remove first and last line and put data to a file called /tmp/port.out.txt
Code:
grep -vE "^$|Port" file.txt >/tmp/port.out.txt
View data
Code:
cat /tmp/port.out.txt
Output:
Code:
0    0   101
0    0   100
Use following shell login to read each line data into a variable called F1, F2, F3.
Code:
n=1
INFILE="data.txt"
OUT="/tmp/port.out.txt"
grep -vE "^$|Port" $INFILE >$OUT
while read line
do
  F1=$(echo $line | awk '{ print $1}')
  F2=$(echo $line | awk '{ print $2}')
  F3=$(echo $line | awk '{ print $3}')
  # use $F1, $F2, $F3 with IF command
  echo "Data for line # $n is $F1 $F2 $F3"
  n=$(expr $n  + 1)
done < $OUT
Note I am using BASH shell if you are using different shell you may need to change commands. But logic remains the same.
__________________
Vivek | My personal blog
Linux Evangelist
Play hard stay cool
Reply With Quote
  #5 (permalink)  
Old 10-09-2006, 03:59 PM
Junior Member
User
 
Join Date: Oct 2006
Posts: 5
Rep Power: 0
fahur
Default

BiG thanks!

I've made some changes for my system and it worked perfectly

Thanks again...

--
Rgds
fahur
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Read arguments from a file and pass them to binary file AHJ Shell scripting 1 10-31-2007 07:04 PM
Problem blocking file ext downloads from Gmail in squid Roshu Networking, Firewalls and Security 1 08-25-2007 12:21 AM
/etc/crontab file permissions or ownership can cause problem chiku Linux software 2 12-20-2006 02:57 AM
convert pdf (ppt file) into wordppt file sureshbup Linux software 1 12-07-2006 04:51 PM


All times are GMT +5.5. The time now is 06:37 PM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36