Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

working on OS X 10.8.3, have part of what I want working, hoping for help with the rest

#!/bin/bash
# script accepts a path to base dir - base path to extract
# also accepts second param - archive /xys/there/usefulLib-version-3.2.1.zip
# 1. cd to base
# 2. get name path of file, 'useful-lib-version-3.2.1.zip'
# 3. strip away extn and -._ spaces so its 'usefulLibversion321' if possible make init char capital of each token (from second) before removing the separator like 'usefulLibVersion321'
# 4. if this folder exists in base then add 1 or 2 or 3 till we get a new folder name, create that. cd to that new folder
# 5. give extract command to original file here (like jar xp -file- or other)


cd $1
file1=$2

file1fullname=$(basename $file1)
file1name=${file1fullname%.*}


echo ${file1fullname}

echo ${file1name}
file1sname=${file1name//./}

file1sname=${file1sname//-/}

file1sname=${file1sname//_/}
file1sname=${file1sname// /}
echo ${file1sname}

mkdir ${file1sname}
cd ${file1sname}
#could use other extract command, i know this one of java
jar xf $2

I need help with point 4.

Motivation : quickly expand the many jars and other archives that I download. some times get many .jar files with spring and other frameworks and utilities.

share|improve this question
    
Normally we don't move something until it's been several weeks without an answer. In this case, it's really a pretty simple case of getting a list of files that start with xyz* and incrementing the last digit(s) as desired which could be done in perl, bash, awk, sed, or quite a few other unix tools. –  bmike Jun 10 '13 at 12:43
1  
Looking at what I think you ask, I would probably extract the jar to a temporary directory first and check for errors, and then move that directory into place by making a function to determine the proper folder number. Unless you are really convinced that xxx1, xxx2, xxx3, xxx4 is the proper pattern for naming folders - using a date or version stamp might be easier to code and work with, since it's not clear how you want to deal with 9->10 and 99->100 in your name collision detection code (assuming you don't want a collision with xxx1 to become xxx11 and then xxx111). –  bmike Jun 10 '13 at 13:05
    
yes date is fine too and for the last number part can add _1, _9 so it goes to _10 ... baseName_2013_06_10_11 sorry i left out the /bin/sh it is a bash script @bmike –  tgkprog Jun 10 '13 at 13:34

1 Answer 1

up vote 2 down vote accepted

How about the date naming below and an attempt to ensure no more than one collision of names happens. You could also add seconds to the mix, but this seems more than robust enough for most cases.

DATE=$(date +"%Y%m%d%H%M")
final=${file1sname}_${DATE}
if [-a $final]
  then final=${file1sname}_${DATE}_1
  sleep 60 
  #we will prevent another run of this tool in the current minute 
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.