----- Original Message -----
From: "Nick Zeljkovic"
Your cut command looks like it's going to give you "/etc/apache2/sites" every time. You should sort your result because ls garantees no particular order unless using ie -lt which is time anyways not numerical. You don't need expr as long as you are using bash or ksh or zsh or almost anything but stock sh on sco unix or solaris.
There *shouldn't* be anything else there except sites in format like: 001-site1 002-site2
Etc.. The cut actually works OK, if it doesn't find anything, it just gives empty string which I address later on.
You're right, I forgot that you were cutting the output of basename, which would have removed the "-" from earlier in the string "sites-enabled". Sorry 'bout that.
#!/bin/bash unalias ls ## grr F*&^* distros... A=`cd /etc/apache2/sites-enabled ;ls |cut -d- -f1 |sort -n |tail -1` A=`printf "%03i\n" $((++A))`
In ksh you don't need to spawn a sub shell just to use printf just to get it back to right-justifid, zero-padded, 3 digits. Nor do you need the subshell nor any of the processes in the backticks with ls either as long as we're making any assumptions about the filenames anyways. There is a quirk that you must start with 001 though. You can have a file 000-site0, and other files of other forms, just this script will never see those.
That's exactly what I've been looking for, somewhat, as 000-site0 should be on servers already, as it will serve default, non named-based access.
#!/bin/ksh typeset -Z3 A=001 cd /etc/apache2/sites-enabled while [ -e ${A}-* ] ;do ((++A)) ;done
In both cases $A holds the new/next value at the end echo Next site is $A
Thanks!
Ain't no thang. Also something to be aware of, maybe you'll consider this a feature or may consider it a problem: This will always find the next available number, not necessarily the next higher number. If you create 001 through 023, and then delete 011, the next time you run this it will give you 011, not 024. Then it would give 024 the next time after that. You could write a different while-loop that scans all possible numbers and gives you the next after the highest found: #!/bin/ksh typeset -Z3 A=001 n=001 cd /etc/apache2/sites-enabled while [ $n -le 999 ] ;do [ -e ${n}-* ] && A=$n ;((++n) ;done ((++A)) Next site is $A n loops from 001 all the way to 999. A gets updated to be a copy of n every time any file is found. At the end of the loop, A will be the same as the highest file, so we incriment A once after the loop. But that could still re-use numbers if you happen to delete numbers from the high end. If you create 001 through 023 and then delete 018 through 023, it will tell you 018 on the next run. You'd need to use a file to hold a running value to really never re-use a number. But then you don't need any counter loop any more either. #!/bin/ksh typeset -Z3 A=001 C=/etc/apache2/last-site.txt read A < $C echo $((++A)) > $C Next site is $A And this is the kind of thing where you probably should not re-use numbers, just as you should generally not delete users but instead "retire" or disable them. And whether deleted or retired, you should never re-use the UID. Make the number 6 digits and make the last-site file a single file on a single admin server and instead of "read < file" and "echo > file", create a small cgi on the central admin box that can do the incriment and re-write and echo back the number. Then have all other boxes read the number from the cgi via wget or curl or even netcat, or even the tcp features built in to bash/ksh/zsh. Sounds fancy but really it's hardly more than what you're already doing. This is all it would take: /srv/www/cgi-bin/next-site on the admin box: #!/bin/bash echo -e "content-type: text/plain\n" [ "$QUERY_STRING" = "seeqrit" ] || exit 1 c=last-site.txt [ -e $c ] || echo 0 >$c read n < $c echo $((++n)) > $c printf "%06i\n" $n On every other box you run: A=`curl -s http://adminbox/cgi-bin/next-site?seeqrit` echo Next site is $A The cgi doesn't need ksh in this case. We aren't scanning file names, and so we don't need to work with the formatted form of the number, and so the number we do math on and store in the file can be a plain integer, or a plain untyped variable. And the printf is fine for echoing it back to the client. This way handles the missing, 000000, & 000001 cases too. Now no two sites will ever use the same number across all your boxes, and no number will ever get re-used. This is much saner for accounting and administration and maintenance too like if you need to transfer a site, or all sites from one box to another, no problem, none will ever clobber any other. -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org