LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2015, 06:55 PM   #1
getraj.12
LQ Newbie
 
Registered: Jul 2010
Location: Bangalore, India
Distribution: RedHat, Suse, Solaris
Posts: 15

Rep: Reputation: 0
Shell Script : Move files and check


I have written a simple move_script to check and move the files from multiple dirs to other dirs and a check_script scheduled via cron every 30 mins to check and runs move_script if it is not running.

The files gets moved without any issues but the script doesn't exits once the move is completed and the cron job i have scheduled finds the script is still in running state and it doesn't do anything.

MOVE SCRIPT :
Code:
#!/bin/sh
while [ Y = Y ]
sleep 600
do
 ls -l /Prod/ABC/*txt* > /dev/null 2>&1
 if [ $? -eq 0 ]
 then
 sleep 60
 for i in `ls -1 /Prod/ABC/*txt*`
  do
  echo "Moving file $i to /NFS_share/Prod/ABC/txt"  >> /var/log/move_script.log
  mv $i /NFS_share/Prod/ABC/txt >> /var/log/move_script.log 2>&1
 done
 fi

 ls -l /Prod/XYZ/*txt* > /dev/null 2>&1
 if [ $? -eq 0 ]
 then
 sleep 60
 for i in `/Prod/XYZ/*txt*`
  do
  echo "Moving file $i to /NFS_share/Prod/XYZ/txt" >> /var/log/move_script.log
  mv $i /NFS_share/Prod/XYZ/txt >> /var/log/move_script.log 2>&1
 done
 fi

 ls -l /Prod/ABC/*doc* > /dev/null 2>&1
 if [ $? -eq 0 ]
 then
 sleep 60
 for i in `ls -1 /Prod/ABC/*doc*`
  do
  echo "Moving file $i to /NFS_share/Prod/ABC/doc" >> /var/log/move_script.log
  mv $i /NFS_share/Prod/ABC/doc >> /var/log/move_script.log 2>&1
 done
 fi

 ls -l /Prod/XYZ/*doc* > /dev/null 2>&1
 if [ $? -eq 0 ]
 then
 sleep 60
 for i in `ls -1 /Prod/XYZ/*doc*`
  do
  echo "Moving file $i to /NFS_share/Prod/XYZ/docl" >> /var/log/move_script.log
  mv $i /NFS_share/Prod/XYZ/doc >> /var/log/move_script.log 2>&1
 done
 fi

done
CHECK_SCRIPT
Code:
ps -ef |grep move_script.sh |grep -v $$ > /dev/null 2>&1
if [ $? -ne 0 ]
then
nohup /scripts/move_script.sh &
fi
Kindly request to check and advice me on whats wrong with the script. Please let me know how to log the o/p of the scripts along with the time stamp. Also kindly suggest me any better way of doing this tasks as i am new to scripting. Appreciate your response.
 
Old 01-28-2015, 07:08 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
I have not looked deeply into your script, but this outer loop...

Code:
#!/bin/sh
while [ Y = Y ]
do
...
done
...will never terminate, so your script will indeed run forever.

'Y' does not appear to be defined within the script, so it is not clear what that means in any event.
 
1 members found this post helpful.
Old 01-28-2015, 11:06 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
As a start:
old:
Code:
for i in `ls -1 /Prod/ABC/*txt*`
  do
  echo "Moving file $i to /NFS_share/Prod/ABC/txt"  >> /var/log/move_script.log
  mv $i /NFS_share/Prod/ABC/txt >> /var/log/move_script.log 2>&1
 done
new:
Code:
mv -v -t /NFS_share/Prod/ABC/txt /Prod/ABC/*txt* >>/var/log/move_script.log 2>&1
 
  


Reply

Tags
files, move, shell scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell Script to rename and move files to a different folder in Windows Simon1984 Linux - Newbie 10 05-18-2012 10:50 AM
Shell Script for Identifying the file and zip all files, move the files to Target Dir gvenkat Linux - Newbie 3 05-07-2011 10:53 AM
[SOLVED] Check folder for specific files than move script BlackCrowe Programming 3 11-23-2010 05:59 AM
Help: Create a shell script to move only files wich has stopped growing proxmity Linux - Newbie 8 07-28-2009 12:40 PM
Very simple shell script - move files to different directories beley Programming 7 11-02-2006 05:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration