nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Wanting to check before running...

This is a discussion on Wanting to check before running... within the Shell scripting forums, part of the Development/Scripting category; The following code is designed to clean out any unwanted "._" Mac OS specific files which have accidentally gotten included ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-12-2009, 10:37 AM
Junior Member
User
 
Join Date: Dec 2009
OS: Debian
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
meesto is on a distinguished road
Default Wanting to check before running...

The following code is designed to clean out any unwanted "._" Mac OS specific files which have accidentally gotten included in a number of Zip archives. There are 247 files and this seems a good place to use some scripting to do the work. The following seems to work OK, but before I run it on the entire lot of files I thought it would be good to have someone who is more experienced look over my code. I do not have much experience with Bash and don't want to make a boatload of unwanted problems for myself.
Code:
#!/bin/bash

# init
function pause(){
     read -p "$*"
}

for f in `ls multipart/`
do
  if [[ "$f" == *.zip ]]
  then
    echo "Processing $f file..."
    mv multipart/$f multipart/__temp/    # Move zip file to temp dir.
    unzip multipart/__temp/*.zip         # Unzip archive.   
    rm multipart/__temp/*.zip            # Remove the old zip archive.
    rm multipart/__temp/._*.mp3          # Remove the Apple "._" files.
    zip -r multipart/__temp/$f *.mp3     # Zip the mp3 files to a new archive.
    mv multipart/__temp/$f multipart/    # Move the new archive back.                   
    rm multipart/__temp/*                # Remove any other temp files.     
    pause 'Press any key to continue...'
  fi
done


Is there any reason to be concerned about running the above script on a Linux-based web server?

I've found that if I interrupt the script and cancel it, and restart it that it seems to append to the original files and I end up with new archives which are twice as big as the originals. This does not seem right and I wonder if there is something obvious that I am doing wrong.

Thanks for your time.

Kind regards,
Chris

Reply With Quote
  #2 (permalink)  
Old 05-12-2009, 10:21 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,707
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

I think for loop can be replaced with a find command and while loop:
Code:
for f in `ls multipart/`
This will handle file names with spaces and other characters:
Code:
find /path/to/multipart -iname "*.zip" -print0  |  while read -d $'\0' file
do
  echo "Something on $file..."
  # note $file is double quoted
  mv "$file" multipart/__temp/    # Move zip file to temp dir.
  # add rest of the commands  
done
As usual test with dummy data in some other directory.
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 07-12-2009, 05:55 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 78
Thanks: 0
Thanked 14 Times in 14 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by meesto View Post
The following code is designed to clean out any unwanted "._" Mac OS specific files which have accidentally gotten included in a number of Zip archives. There are 247 files and this seems a good place to use some scripting to do the work. The following seems to work OK, but before I run it on the entire lot of files I thought it would be good to have someone who is more experienced look over my code. I do not have much experience with Bash and don't want to make a boatload of unwanted problems for myself.
Code:
#!/bin/bash

# init
function pause(){

The word function is redundant and not portable:

Code:
pause() {
Quote:
Code:
     read -p "$*"
}

for f in `ls multipart/`

Not only is ls unnecessary, but it will break your script if any filenames contain whitespace. Use:

Code:
for f in *
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Running php as root Leszek.T Ubuntu / Debian 9 01-10-2009 05:53 PM
Newbie running shall script houssam_ballout Shell scripting 1 21-06-2009 04:08 PM
Getting Squid 3.0 up and running Mic Linux software 4 18-05-2008 03:56 PM
how to enumerate currently running process hiimsa Coding in General 2 11-11-2006 03:02 PM
How long the sys was running? jithendra Linux software 2 16-10-2006 05:31 PM


All times are GMT +5.5. The time now is 10:42 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38