Operating System - HP-UX
1752790 Members
6211 Online
108789 Solutions
New Discussion юеВ

Problem with posix script

 
Tom Ellis
New Member

Problem with posix script

Hello folks, im having problems with a one-line script. it first checks if it is on a HP-UX system, then copies and zips syslog.log and mail.log in /var/adm/syslog/ - it then empties the old files. it also puts the date into the names of the backed up files.

the original series of commands is:


IVO=`uname -s`; if [ "$IVO" != "HP-UX" ]; then echo "Not a HP-UX system...exiting.."; sleep 1; exit 1; fi; echo "Backing up, zipping and emptying syslog.log and mail.log"; cd /var/adm/syslog/; bdf .; for i in *.log; do j=OLD`expr "$i" : "\(.*\).log"`; cp $i $j`date -u +%e-%m-%Y`.log; > $i; gzip -9 $j`date -u +%e-%m-%Y`.log; bdf .; done


If I split this in 2 - it works perfectly -- watch:



itowks02:/var/adm/syslog (root)
itowks02:/var/adm/syslog (root) pwd
/var/adm/syslog
itowks02:/var/adm/syslog (root) ll
total 230
-rw-r--r-- 1 root sys 20080 Oct 24 16:51 mail.log
-rw-r--r-- 1 root sys 9090 Oct 17 23:08 mail.log.Fri
-rw-r--r-- 1 root sys 10038 Oct 20 23:08 mail.log.Mon
-rw-r--r-- 1 root sys 9768 Oct 18 23:30 mail.log.Sat
-rw-r--r-- 1 root sys 9089 Oct 19 23:08 mail.log.Sun
-rw-r--r-- 1 root sys 10040 Oct 23 23:08 mail.log.Thu
-rw-r--r-- 1 root sys 9124 Oct 21 23:08 mail.log.Tue
-rw-r--r-- 1 root sys 9081 Oct 22 23:08 mail.log.Wed
-rw-r--r-- 1 root root 19164 Oct 24 16:51 syslog.log
drwxr-xr-x 2 root sys 1024 Apr 9 2000 trimlogs

itowks02:/var/adm/syslog (root) IVO=`uname -s`; if [ "$IVO" != "HP-UX" ]; then echo "Not a HP-UX system...exiting.."; sleep 1; exit 1; fi; echo "Backing up, zi
pping and emptying syslog.log and mail.log"; cd /var/adm/syslog/; bdf .
Backing up, zipping and emptying syslog.log and mail.log
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol3 8282112 2097055 5798649 27% /
itowks02:/var/adm/syslog (root)
itowks02:/var/adm/syslog (root)

itowks02:/var/adm/syslog (root) for i in *.log; do j=OLD`expr "$i" : "\(.*\).log"`; cp $i $j`date -u +%e-%m-%Y`.log; > $i; gzip -9 $j`date -u +%e-%m-%Y`.log; b
df .; done
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol3 8282112 2097033 5798670 27% /
Filesystem kbytes used avail %used Mounted on
/dev/vg00/lvol3 8282112 2097011 5798690 27% /

itowks02:/var/adm/syslog (root) ll
total 142
-rw-r--r-- 1 root sys 1038 Oct 24 16:52 OLDmail24-10-2003.log.gz
-rw-r--r-- 1 root sys 1368 Oct 24 16:52 OLDsyslog24-10-2003.log.gz
-rw-r--r-- 1 root sys 0 Oct 24 16:52 mail.log
-rw-r--r-- 1 root sys 9090 Oct 17 23:08 mail.log.Fri
-rw-r--r-- 1 root sys 10038 Oct 20 23:08 mail.log.Mon
-rw-r--r-- 1 root sys 9768 Oct 18 23:30 mail.log.Sat
-rw-r--r-- 1 root sys 9089 Oct 19 23:08 mail.log.Sun
-rw-r--r-- 1 root sys 10040 Oct 23 23:08 mail.log.Thu
-rw-r--r-- 1 root sys 9124 Oct 21 23:08 mail.log.Tue
-rw-r--r-- 1 root sys 9081 Oct 22 23:08 mail.log.Wed
-rw-r--r-- 1 root root 0 Oct 24 16:52 syslog.log
drwxr-xr-x 2 root sys 1024 Apr 9 2000 trimlogs
itowks02:/var/adm/syslog (root)



But if I put them together:
itowks02:/var/adm/syslog (root)
itowks02:/var/adm/syslog (root) IVO=`uname -s`; if [ "$IVO" != "HP-UX" ]; then echo "Not a HP-UX system...exiting.."; sleep 1; exit 1; fi; echo "Backing up, zi
pping and emptying syslog.log and mail.log"; cd /var/adm/syslog/; bdf .; for i in *.log; do j=OLD`expr "$i" : "\(.*\).log"`; cp $i $j`date -u +%e-%m-%Y`.log; >
$i; gzip -9 $j`date -u +%e-%m-%Y`.log; bdf .; done
itowks02:/var/adm/syslog (root) bdf .; for i in *.log; do j=OLD`expr "$i" : "\(.*\).log"`; cp $ <
>
>
> ^C
itowks02:/var/adm/syslog (root)

I think its caused by the number of commands/characters the shell will accept before the next newline - I think I recall running into such a limit before when using C/C++/Pascal.

Syntax is fine (I think) - there is no reason why a string of commands, which works perfectly, followed by another string of commands which work perfectly, seperated by a semicolon, should not work.


It looks as if there is an unmatched quote - I would say this is because the shell only reads so far before (discarding?) the rest. So it might get as far as one quote but discard the second one.

ivo
5 REPLIES 5
Todd McDaniel_1
Honored Contributor

Re: Problem with posix script

First of all if you have a semi-colon in there it is not a oneliner :)... you just put it on one line.

Also, I believe that you have a problem with all this being evaluated as a variable known as IVO.

Your syntax looks fine, however, I count well over 300 characters... if you are trying to pass this output to a variable, I believe there is a 256 limitation on variable assignation.

why dont you simply place your commands in a shell script then export the variable? It seems that will work better.
Unix, the other white meat.
Mark Greene_1
Honored Contributor

Re: Problem with posix script

You are running into commandline limitations within the quoted stuff. Break it up more. In addition to being a horrible read, you aren't gaining any efficiencies by coding it like that.

Also, keep in mind that there will, eventually, be people other than yourself who will have to maintain these scripts if they aren't one-time runs. You have an obligation to your employer to not obfuscate your work to the point where someone else cannot do it.

HTH
mark
the future will be a lot like now, only later
Ivo Mencke
New Member

Re: Problem with posix script

Great, thanks for the help Todd & Mark. I know its a horrible read but it has to run on one line, because I have to be able to copy & paste it into a shell. - i work in support and ITO reports a lot of /var's too full, which I have to reduce to below a threshold. I only have a reflection session and dont want (and am probably not allowed) to put scripts on the customer servers. Also, for some reason I show up as Mark Ellis in ITRC forums, even though my name is, and I am registered as Ivo Mencke.

thanks again,

ivo.
Graham Cameron_1
Honored Contributor

Re: Problem with posix script

Ivo

Even if you can't or don't want to put scripts onto the server, maybe you have the ability to edit your own .profile?

If so, why don't you code your main processing into a function ? At the end of your .profile, add

function backupandzip
{
echo "Backing up, zipping and emptying syslog.log and mail.log"
cd /var/adm/syslog/
bdf .
for i in *.log
do j=OLD`expr "$i" : "\(.*\).log"`
cp $i $j`date -u +%e-%m-%Y`.log
> $i
gzip -9 $j`date -u +%e-%m-%Y`.log
bdf .
done

}

You can also abbreviate the test clause and your on-liner can then become:

[[ $(uname -s) = "HP-UX" ]] && backupandzip || echo "Not HP-UX ..."

Hope this helps

-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Elmar P. Kolkman
Honored Contributor

Re: Problem with posix script

The reason it is not working in a single line has to do with the shell, which replaces the wild-chars. You do multiple multiple times a '*.log', which will be replaced the moment you run that command line. If everything is in one command line, they will be replaced by the same filenames. But you alter them in the first for loop.

The best way is to make it an alias or script. It depends on the way you login to the client machine on what your best solution might be. You could, for instance, copy the script to /tmp, run it, and remove it, leaving no rubbish on the clients machine but still do your stuff automatically. If the script is tested well, this will make it a lot saver then to copy-and-past.

Just my 2 cents...
Every problem has at least one solution. Only some solutions are harder to find.