View Full Version : shell script problem
NicoV 04-16-2003, 06:35 AM Hi,
I've been working on several shell scripts for a class that I am following, and I am really stuck on one of the questions. I was wondering if somebody could point me in the right direction as to how to solve this problem. (note: i dont need the whole solution, just a few pointers)
This is the problem:
Write a script that will generate a list containing the words from the file words.dutch.small, including the amount of bytes that each woord consists of. The list should be sorted by the amount of bytes each word is made up of.
Hint: Look at the manual page of wc
Hint: Use command substitution in some way
Note: the words.dutch.small file has one word per line and is sorted alphabetically.
The problem I am stuck on for now is that i cant get wc to work on a single word. If i pass it a word it thinks it is a filename. And we arent allowed to make files in our shell scripts.
NicoV 04-16-2003, 08:09 AM nobody can help me out a bit? :(
this is the code i have right now, but it isn't working as wc sees $i as a file:
for i in `cat words.dutch.small`
do
$bytes=`wc -c < $i`
echo $bytes, $i
done
jb4mt 04-16-2003, 08:31 AM why use wc on a word when you know it is expecting a file name? what you need to do is get the LENGTH of the word, so the length of your variable $i.
sorry I don't know how to do this off the top of my head, my bash book is buried somewhere, but I hope this helps you figure out what to research.
sasha 04-16-2003, 08:38 AM for i in `cat words.dutch.small`
do
bytes=`echo -n $i | wc -c `
echo $bytes, $i
done
jb4mt 04-16-2003, 09:02 AM Originally posted by sasha
for i in `cat words.dutch.small`
do
bytes=`echo -n $i | wc -c `
echo $bytes, $i
done
Ha! Good idea, piping $i to wc.
sasha 04-16-2003, 09:10 AM if you like piping , you will like this:
for i in `cat words.dutch.small`;do bytes=`echo -n $i | wc -c `; echo $bytes, $i ; done |sort
One thing to note is that -n after echo, without it echo would append newline char wich then counts as extra byte.
NicoV 04-16-2003, 11:52 AM Thx a lot sasha!
It worked perfectly
JustinH 04-16-2003, 04:06 PM Originally posted by NicoV
Thx a lot sasha!
It worked perfectly
I tend to wonder if Sasha even sleeps... he's always here in the programming forum :).
NicoV 04-16-2003, 04:56 PM isn't sasha a she? at least in holland it is considered a girls name
sasha 04-16-2003, 05:27 PM Originally posted by comphosting
I tend to wonder if Sasha even sleeps... he's always here in the programming forum :).
You know when you are looking for something and you cannot find it, and then you stop looking and realize that it was just there in front of you all the time. That is what this forum does for me. I work and have WHT opened in the background. Helping someone here is like a short break, and helps me focus and do my own stuff better.
JustinH 04-16-2003, 05:32 PM Originally posted by sasha
You know when you are looking for something and you cannot find it, and then you stop looking and realize that it was just there in front of you all the time. That is what this forum does for me. I work and have WHT opened in the background. Helping someone here is like a short break, and helps me focus and do my own stuff better.
I know exactly what you are talking about as I do the same thing :). Stepping away from the problem almost always helps :).
PHBPendragon 04-17-2003, 06:34 PM Originally posted by comphosting
Stepping away from the problem almost always helps :).
but running away solves it forever. :)
/me not having a good coding day.............
hostchamp 04-18-2003, 03:12 PM Sasha and all you shell script gurus please let me know if you have a shell script solution to the follwing;
I want to delete the virtaul host blocks to clean up my httpd.conf, doing this manually with vi would take long as there are hundreds of virtual hosts. I need a shell script which can accomplish this task.
I host mutiple domains in one folder, so is there any way the script would take the document root from me as an imput, say "/home/web/viv" as the input and delete all the virtual hosts blocks which are hosted with document root /home/web/viv, for eg. /home/web/viv/domain1.com, /home/web/viv/domain2.com and so on?
FYI my virtual host block is 9lines, i.e. from <virtualhost> to </virtualhost> it is 9 lines in total.
I would not mind specifying each domain name one-by-one if the document root method isn't possible.
PHBPendragon 04-18-2003, 06:32 PM awk '/begin/,/end/' filename
will grab everything between begin & end
perl -p -i -e 's/pattern//'
will delete every line with pattern - brutal, no mercy. make backup copies first.
The actual regexes that work are left as an exercise to the reader.
hostchamp 04-19-2003, 05:31 AM phppendragon thanks for that but it won't delete a virtualhost block, i want something which would take a domain name or better yet the document root as input and then delete the complete virtualhost block for the matching domain name or document root.
PHBPendragon 04-19-2003, 11:50 AM one solution may be the grep -B and grep - A args
-B is back and -A is forward x number of lines.
grep -B 2 <docroot> should grab the first line of the vhost, etx.
To solve this we use an Include directive in the httpd/conf -
Include /usr/local/etc/apache/domains/
and keep every vhost in it's own file
Then we just delete the file. when removing vhosts.
hostchamp 04-19-2003, 02:49 PM okay say i do a grep -B2 /home/user/domain
and also grep -A6 /home/user/domain
now what?
How do i delete the complete virtual host block?
|