Operating System - HP-UX
1830230 Members
2271 Online
109999 Solutions
New Discussion

stacking or pop files script

 
iranzo
Frequent Advisor

stacking or pop files script

Hello,

I look for a shell script
for "stacking" or "pop to" files in a directory , on HPUX server .

Thanks .
Bonjour
4 REPLIES 4
Mark Grant
Honored Contributor

Re: stacking or pop files script

For me, you are going to have to explain what you mean by "stacking" or "pop to".
Never preceed any demonstration with anything more predictive than "watch this"
iranzo
Frequent Advisor

Re: stacking or pop files script

sorry :
here a file : /tmp/toto
i receive another file "toto"
then the script "stacking" the file :
(rename the file )
now here 2 files /tmp/toto and /tmp/toto.1

"pop to" is the inverse function .
Bonjour
Mark Grant
Honored Contributor

Re: stacking or pop files script

If I understand you correctly, this script will do it. Save it as "stackpop"

If you have a file "hello" "hello.1" and "hello.2" and run it as "stackpop stack hello" it will mv "hello" to "hello.3"

If you the run it as "stackpop popto hello" it will move "hello.3" to "hello".

It doesn't do any checking for errors and will happily overwrite things. You might need to adjust it a bit.

#!/usr/bin/perl

($action,$FILENAME)=@ARGV;
@files=glob "$FILENAME*";
($suffix)=(pop(@files)=~/$FILENAME.(\d+)/);

eval($action);

sub stack{
$suffix++;
`mv $FILENAME ${FILENAME}.$suffix`;
}

sub popto {
`mv ${FILENAME}.$suffix $FILENAME`;
}

Never preceed any demonstration with anything more predictive than "watch this"
Seth Parker
Trusted Contributor

Re: stacking or pop files script

While not strictly a true push/pop implementaion, there was nothing wrong with Marks's solution. You'd just have to have the calling script know that the highest numbered suffix was the most recent.

Anyway, how about this, since you asked for a shell script:

****************
push.sh (stack)
****************

typeset -i x

if [ ! -f $1 ]
then
exit 1
fi

for FILE in $(l -1r ${1}*)
do
if [ $FILE != $1 ]
then
x=$(echo $FILE | awk -F. '{print $NF}')+1
else
x=1
fi

mv $FILE $1.$x
done


****************
pop.sh
****************

typeset -i x

if [ ! -f $1 ]
then
exit 1
fi

for FILE in $(l -1 ${1}.*)
do
x=$(echo $FILE | awk -F. '{print $NF}')-1

if [ $x -ne 0 ]
then
mv $FILE $1.$x
else
mv $FILE $1
fi
done

****************

The error checking is weak, but it will error out if there is no base name. One thing I did provide for is multiple "." in a filename, so "toto.tmp" will work just as well as "toto". Call the scripts as "stack.sh toto" and "pop.sh toto" and they'll do what you asked.

Regards,
Seth