Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]Script Writing Help[/b] 1

Status
Not open for further replies.

sraj142

Technical User
Oct 25, 2006
49
0
0
IN
Hi All,
I need help to write a script based on the following requirement.

The script will scan the system time. It will move *.* from /home/test1 to /home/backup/. There will be files e.g. laser1, laser2, laser3 ... in the target folder. If the current system time is 7:30, the script will move all the files with "_a" as suffix with all the file names like laser1_a, laser2_a, laser3_a......
If the current system time is 8:30, the script will move all the files with "_b" as suffix with all the file names like laser1_b, laser2_b, laser3_b......
If the current system time is 9:30, the script will move all the files with "_c" as suffix with all the file names like laser1_c, laser2_c, laser3_c.....

There will be no "suffix like _a, _b & _c" in the target directory. But the script will rename all the files with proper suffix & then move them to destination.

Can anybody please help me to write the script ?

Regards - Sraj
 
So exactly what help do you need?

pseudo:

now = date +"%H%M"
case now
0730: suffix = _a
0830: suffix = _b
0930: suffix = _c

find /sourcepath -type f, for each:
filename = basename of current file
move filename to new path with suffix


Cake.
 
Hi Chapter11,
Thanks for the reply. I need a script to so. For example I have mentioned 3 files, but the target directory have more then 200 files everytime which I wanted to move to the destination with a suffix (based on the given time) to identify those files.
Actually I am new to scripting & need help....

Sraj
 
You can do it this way:

Code:
filenames=$(ls -al /home/test | grep ^- |grep 07:30 | awk '{print $9}')

for i in $filenames
do
mv /home/test/$i /home/backup/$i"_a"
done

filenames=$(ls -al /home/test | grep ^- | grep 08:30 | awk '{print $9}')

for i in $filenames
do
mv /home/test/$i /home/backup/$i"_b"
done

filenames=$(ls -al /home/test | grep ^- | grep 09:30 | awk '{print $9}')

for i in $filenames
do
mv /home/test/$i /home/backup/$i"_c"
done

Regards,
Khalid
 
To refine the above a bit more (though i beleive that there are simpler solution to this), you can use this:

Code:
filenames=$(ls -al /home/test | grep ^- | awk '$8 ~ /07:30/' | awk '{print $9}')

for i in $filenames
do
mv /home/test/$i /home/backup/$i"_a"
done

filenames=$(ls -al /home/test | grep ^- | awk '$8 ~ /08:30/' | awk '{print $9}')

for i in $filenames
do
mv /home/test/$i /home/backup/$i"_b"
done

filenames=$(ls -al /home/test | grep ^- | awk '$8 ~ /09:30/' | awk '{print $9}')

for i in $filenames
do
mv /home/test/$i /home/backup/$i"_c"
done

This is just to make sure that there are no filenames that contains 07:30 or 08:30 or 09:30 within the string of the filename.

But as i said i know people who will give you only three lines instead of the above :) this is only one way to do it.

Regards,
Khalid
 
Hi Khalid,
Thanks for the tips....
oops ! I did a mistake earlier i.e. the time stamp is not exactly 7:30 or 8:30, but a range like files created on or before 7:30.
Actualy, the best description may be the following.....

-> when the time is 7:30 move *.* with "_a" suffix with all the files to the target.
-> when the time is 8:30 move *.* with "_b" suffix with all the files to the target.

hope to get a solution

-Sraj
 
hi Sraj,

ok let me just clarify your need now! would it be this way:

when time <= 7:30
move every thing in /home/test with "_a" suffix to /home/backup
when time > 7:30 and <= 8:30
move every thing in /home/test with "_b" suffix to /home/backup
when time > 8:30 and <= 9:30
move every thing in /home/test with "_c" suffix to /home/backup

Is that what you are saying? then what about the files > 9:30?

Regards,
Khalid
 
Hi Khalid,
Thanks bro for your reply....
Yea you are right. Actually I have just mentioned 3 schedule, but all the schedules are as follows...
when time <= 7:30
move every thing in /home/test with "_a" suffix to /home/backup
when time > 7:30 and <= 8:30
move every thing in /home/test with "_b" suffix to /home/backup
when time > 8:30 and <= 9:30
move every thing in /home/test with "_c" suffix to /home/backup
when time > 9:30 and <= 11:30
move every thing in /home/test with "_d" suffix to /home/backup
when time > 11:30 and <= 12:30
move every thing in /home/test with "_d" suffix to /home/backup
when time > 12:30 and <= 14:00
move every thing in /home/test with "_c" suffix to /home/backup
when time > 14:00 and <= 15:30
move every thing in /home/test with "_c" suffix to /home/backup
when time > 15:30 and <= 16:30
move every thing in /home/test with "_c" suffix to /home/backup
when time > 16:30 and <= 17:00
move every thing in /home/test with "_c" suffix to /home/backup

After 17:00, the schedule will be after every 30 mins till upto 21:00 hrs...

Thanks in advance

-Sraj
 
oops !!!

The following "suffixes" will be changed accordingly like "_a", "_b", "_c", "_d", "_e"......upto "_q"

-Sraj
 
Unfortunately i don't have access to an aix box as its weekend :) so i will do it once i be at work!

Just to get this straight once more

Code:
when time <= 7:30
  move every thing in /home/test with "_a" suffix to /home/backup
when time > 7:30 and <= 8:30
  move every thing in /home/test with "_b" suffix to /home/backup
when time > 8:30 and <= 9:30
  move every thing in /home/test with "_c" suffix to /home/backup
when time > 9:30 and <= 11:30
  move every thing in /home/test with "_d" suffix to /home/backup
when time > 11:30 and <= 12:30
  move every thing in /home/test with "_e" suffix to /home/backup
when time > 12:30 and <= 14:00
  move every thing in /home/test with "_f" suffix to /home/backup
when time > 14:00 and <= 15:30
  move every thing in /home/test with "_g" suffix to /home/backup
when time > 15:30 and <= 16:30
  move every thing in /home/test with "_h" suffix to /home/backup
when time > 16:30 and <= 17:00
  move every thing in /home/test with "_i" suffix to /home/backup
when time > 17:00 and <= 17:30
  move every thing in /home/test with "_j" suffix to /home/backup
when time > 17:30 and <= 18:00
  move every thing in /home/test with "_l" suffix to /home/backup
when time > 18:00 and <= 18:30
  move every thing in /home/test with "_m" suffix to /home/backup
when time > 18:30 and <= 19:00
  move every thing in /home/test with "_n" suffix to /home/backup
when time > 19:00 and <= 19:30
  move every thing in /home/test with "_o" suffix to /home/backup
when time > 19:30 and <= 20:00
  move every thing in /home/test with "_p" suffix to /home/backup
when time > 20:00 and <= 20:30
  move every thing in /home/test with "_q" suffix to /home/backup
when time > 20:30 and <= 21:00
  move every thing in /home/test with "_r" suffix to /home/backup

Would it be this way? Please confirm!

Regards,
Khalid
 
Yea Khalid....you are right

Regards,
Sraj
 
hence, case on current time to set a suffix, and make the move generic.

Code:
SRCDIR="/home/test"
DSTDIR="/home/backup
NOW=`date +"%H%M"
case "${NOW}" in
   "0730":
      SUFFIX="_a"
      ;;
   *)
      echo "Unhandled Time"
      exit
      ;;
esac

find ${SRCDIR} -type f | while read PFN
do
   FN=`basename ${PFN}`
   mv ${PFN} ${DSTDIR}/${FN}_${SUFFIX}
done

Add more times in the case statement as you like.
 
There are two errors.

Close the backtick on the "NOW=" line.
Remove the underscore in the mv line.
 
Hi Chapter11,
Thanks for the reply. It is a bit hard to me to understand your script. Can you please suggest some documentation to learn the scripting well.

Regards,
Subharaj
 
Sraj,

I haven't forgotten about your case! I tried for a while but i'm currently busy :) I will come back to you with a solution soon hopefully.

scripting documentation is every where on the net but this is one site i used for scripting:


(just hit next for the next topic!!!)

Regards,
Khalid
 
Thanks a lot Khalid...will be waiting for your reply.....

Rgds - Sraj
 
Hi Khalid,

I am still waiting for you.....May I know when you could get some time for me ???


Regards- Sraj


 
Oh sorry Sraj. The problem that i'm sick leave today and i don't have an aix box to try it :)

But i will try to sit down and write a code, pass it to you, and you can run it on your server and let me know if it works! how about that?

Be right back soon :)

Regards,
Khalid
 
wow! :)

For someone who is on sick leave this wasn't something that easy to be done but i did it for you coz i promised doing so :)

As i said i don't have an aix box so you better double check or even triple check for errors!

Let me know how it goes with you. I will be waiting...

Code:
ls -al /home/test | grep ^- | while read i
do
ftime=$(echo $i | awk '{print $8}')
fname=$(echo $i | awk '{print $9}')
fhr=$(echo $ftime | cut -d':' -f1)
fmin=$(echo $ftime | cut -d':' -f2)
if [[ $fhr -lt 07 || $fhr -eq 07 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_a"
elseif [[ $fhr -eq 07 && $fmin -gt 30 || $fhr -eq 08 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_b"
elseif [[ $fhr -eq 08 && $fmin -gt 30 || $fhr -eq 09 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_c"
elseif [[ $fhr -eq 09 && $fmin -gt 30 || $fhr -eq 11 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_d"
elseif [[ $fhr -eq 11 && $fmin -gt 30 || $fhr -eq 12 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_e"
elseif [[ $fhr -eq 12 && $fmin -gt 30 || $fhr -lt 14 || $fhr -eq 14 && $fmin -eq 00 ]]
mv /home/test/$fname /home/backup/$fname"_f"
elseif [[ $fhr -eq 14 || $fhr -eq 15 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_g"
elseif [[ $fhr -eq 15 && $fmin -gt 30 || $fhr -eq 16 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_h"
elseif [[ $fhr -eq 16 && $fmin -gt 30 || $fhr -eq 17 && $fmin -eq 00 ]]
mv /home/test/$fname /home/backup/$fname"_i"
elseif [[ $fhr -eq 17 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_j"
elseif [[ $fhr -eq 17 && $fmin -gt 30 || $fhr -eq 18 && $fmin -eq 00 ]]
mv /home/test/$fname /home/backup/$fname"_l"
elseif [[ $fhr -eq 18 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_m"
elseif [[ $fhr -eq 18 && $fmin -gt 30 || $fhr -eq 19 && $fmin -eq 00 ]]
mv /home/test/$fname /home/backup/$fname"_n"
elseif [[ $fhr -eq 19 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_o"
elseif [[ $fhr -eq 19 && $fmin -gt 30 || $fhr -eq 20 && $fmin -eq 00 ]]
mv /home/test/$fname /home/backup/$fname"_p"
elseif [[ $fhr -eq 20 && $fmin -le 30 ]]
mv /home/test/$fname /home/backup/$fname"_q"
elseif [[ $fhr -eq 20 && $fmin -gt 30 || $fhr -eq 21 && $fmin -eq 00 ]]
mv /home/test/$fname /home/backup/$fname"_r"
fi
done

Regards,
Khalid
 
Going thru the commands, i found that i typed elseif instead of elif :) I guess that's one of the drawbacks of working on many programming languages :)

Code:
ls -al /home/test | grep ^- | while read i
do
ftime=$(echo $i | awk '{print $8}')
fname=$(echo $i | awk '{print $9}')
fhr=$(echo $ftime | cut -d':' -f1)
fmin=$(echo $ftime | cut -d':' -f2)
if [[ $fhr -lt 07 || $fhr -eq 07 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_a"
elif [[ $fhr -eq 07 && $fmin -gt 30 || $fhr -eq 08 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_b"
elif [[ $fhr -eq 08 && $fmin -gt 30 || $fhr -eq 09 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_c"
elif [[ $fhr -eq 09 && $fmin -gt 30 || $fhr -eq 11 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_d"
elif [[ $fhr -eq 11 && $fmin -gt 30 || $fhr -eq 12 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_e"
elif [[ $fhr -eq 12 && $fmin -gt 30 || $fhr -lt 14 || $fhr -eq 14 && $fmin -eq 00 ]]
then
mv /home/test/$fname /home/backup/$fname"_f"
elif [[ $fhr -eq 14 || $fhr -eq 15 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_g"
elif [[ $fhr -eq 15 && $fmin -gt 30 || $fhr -eq 16 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_h"
elif [[ $fhr -eq 16 && $fmin -gt 30 || $fhr -eq 17 && $fmin -eq 00 ]]
then
mv /home/test/$fname /home/backup/$fname"_i"
elif [[ $fhr -eq 17 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_j"
elif [[ $fhr -eq 17 && $fmin -gt 30 || $fhr -eq 18 && $fmin -eq 00 ]]
then
mv /home/test/$fname /home/backup/$fname"_l"
elif [[ $fhr -eq 18 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_m"
elif [[ $fhr -eq 18 && $fmin -gt 30 || $fhr -eq 19 && $fmin -eq 00 ]]
then
mv /home/test/$fname /home/backup/$fname"_n"
elif [[ $fhr -eq 19 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_o"
elif [[ $fhr -eq 19 && $fmin -gt 30 || $fhr -eq 20 && $fmin -eq 00 ]]
then
mv /home/test/$fname /home/backup/$fname"_p"
elif [[ $fhr -eq 20 && $fmin -le 30 ]]
then
mv /home/test/$fname /home/backup/$fname"_q"
elif [[ $fhr -eq 20 && $fmin -gt 30 || $fhr -eq 21 && $fmin -eq 00 ]]
then
mv /home/test/$fname /home/backup/$fname"_r"
fi
done

Regards,
Khalid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top