Identify script please!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify script please!
# 1  
Old 01-16-2011
Identify script please!

Have a script that I'm trying to adapt for something else, but I do not understand the following lines. Can anyone help?

I know what expr does, `expr 8 + 2`, but:

Code:
FILENAME=`expr //$FILE : '.*/\(.*\)'`
UNPACKDIR=`echo $FILE | sed -e s/$FILENAME//g`

Thanks

Last edited by pludi; 01-17-2011 at 01:26 PM..
# 2  
Old 01-16-2011
/vi

The first statement uses the expr anchored pattern match to remove everything in $FILE upto the last '/' this is pretty much equivalent to basename $FILE

The 2nd statement uses sed to delete all occurances of $FILENAME from the $FILE string, no doubt this is trying to duplicate the output of dirname $FILE, but it would fail if the FILENAME string matches components of the path eg /work_files/mydir/work.file.

You would probably be better of replacing these two lines with:
Code:
FILENAME=$(basename $FILE)
UNPACKDIR=$(dirname $FILE)/


Last edited by Chubler_XL; 01-17-2011 at 12:19 AM.. Reason: UNPACKDIR requires / on end
# 3  
Old 01-17-2011
For some reason when I use your two lines, it tries to unzip two files because there is a space in the file name. Here is the whole script. I adapted it from an unpacking script, so that I could unzip a folder full of zip files:

Code:
#!/bin/sh

DLDIR=/mnt/HD_a2/files
LOG=/mnt/usb/logs/unzip.log
UNZIP=/opt/bin/unzip-unzip

  # check incoming DLDIR
  for FILE in `find $DLDIR -name "*.zip"`; do
    if [ "$FILE" != "*.zip" ]; then
      # shell-fu to extract to path containing the rar
      #FILENAME=`expr //$FILE : '.*/\(.*\)'`
      #UNPACKDIR=`echo $FILE | sed -e s/$FILENAME//g`
      FILENAME=$(basename $FILE)
      UNPACKDIR=$(dirname $FILE) 
      echo [`date`] Extracting "$FILE" ... >> $LOG

      # unzip file to the directory its sitting in
      $UNZIP "$FILE" -d "$UNPACKDIR" >> /dev/null 2>&1
      echo [`date`] ... done extracting. >> $LOG

      # cleanup - remove the rar file(s)
      # note: match .rar, .r01, .r02 ... etc
      echo [`date`] Removing "$FILENAME and zip files." >> $LOG
      for j in `find "$DLDIR" -name "*.zip"`; do
        rm $j
      done
    fi
  done
  echo [`date`] Done unpacking/extracting \(if any necessary\)... >> $LOG

# fi

exit 0

So if I have /mnt/HD_a2/files/The App.app.zip, then it tries to unzip (and remove) The and App.app.zip which fails. I know there's an easy command but I just don't know how. I have the same issue when I use the original script to unrar files, they cannot have spaces in the filename. Is there also a way to chmod the unzip/unrar FOLDER and its contents to 777 after it unzips/unrars it? Because I have the script running by root, and when I access the files using AFP (under user called nobody) I can't move/delete them.

Thanks for your help!

Last edited by Franklin52; 01-19-2011 at 03:22 AM.. Reason: Please use code tags instead of quote tags
# 4  
Old 01-17-2011
Code:
FILENAME=$(basename "$FILE")
UNPACKDIR=$(dirname "$FILE")

# 5  
Old 01-17-2011
Ok a couple of questions here firstly lets deal with filenames with spaces.

replace your for loops with:
Code:
find $DLDIR -name "*.zip"` | while read FILE ; do
 
 
find "$DLDIR" -name "*.zip"`| while read j ; do

Any references to $FILE or $j need quotes eg:
Code:
FILENAME=$(basename "$FILE")
UNPACKDIR=$(dirname "$FILE") 
 
rm "$j"

Now for permissions, if your find command supports -print0 you can fix ownership by doing this after the unzip:
Code:
find "$UNPACKDIR" -print0 | xargs -0 chmod 777

If not, you will need to do another while+read block and run chmod:
Code:
find "$UNPACKDIR" -print | while read CHFILE ; do
    chmod 777 "$CHFILE"
done

# 6  
Old 01-18-2011
Here is the modified script with the above suggestions. When I run, I automatically get an unexpected syntax error on the "|" in the first for loop. Did I do something wrong?

Code:
#!/bin/sh
DLDIR=/mnt/HD_a2/files/Apps
LOG=/mnt/usb/logs/unzip.log
UNZIP=/opt/bin/unzip-unzip
export PATH=/ffp/sbin:/ffp/bin:/opt/bin:/opt/sbin$PATH

  # check incoming DLDIR
  for FILE in `find $DLDIR ! -path "*/.AppleDouble/*" -name "*.zip"` | while read FILE ; do
    if [ "$FILE" != "*.zip" ]; then
      # shell-fu to extract to path containing the rar
      FILENAME=$(basename "$FILE")
      UNPACKDIR=$(dirname "$FILE")
      echo [`date`] Extracting "$FILE" ... >> $LOG

      # unzip file to the directory its sitting in
      $UNZIP "$FILE" -d "$UNPACKDIR" >> /dev/null 2>&1
      echo [`date`] ... "$FILE" done extracting. >> $LOG

      #chmod unzipped files
      find "$UNPACKDIR" -print0 | xargs -0 chmod 777
      echo [`date`] Chmodding unzipped files...

      # cleanup - remove the rar file(s)
      # note: match .rar, .r01, .r02 ... etc
      # echo [`date`] Removing "$FILENAME and rar files." >> $LOG
      for j in `find "$DLDIR" ! -path "*/.AppleDouble/*" -name "*.zip"` | while read j ; do
        rm "$j"
      done
     fi
  done
  echo [`date`] Done unpacking/extracting \(if any necessary\)... >> $LOG

exit 0

The error is:

Last edited by Franklin52; 01-19-2011 at 03:23 AM.. Reason: code tags
# 7  
Old 01-18-2011
You don't need the for loop anymore just a while loop, so replace:
Code:
for FILE in `find $DLDIR ! -path "*/.AppleDouble/*" -name "*.zip"` | while read FILE ; do

With:
Code:
find $DLDIR ! -path "*/.AppleDouble/*" -name "*.zip" | while read FILE ; do

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to identify error in the script

cat test3.sh #!/bin/sh set -x while getopts ":n:" opt; do case "$opt" in n) host=$OPTARG shift 2 ;; -h ) host=$2 shift 2 ;; *) break ;; esac done; echo "host =... (7 Replies)
Discussion started by: Vishal_dba
7 Replies

2. Shell Programming and Scripting

Identify variables used in the shell script

Hi, Is there any simple way to get/identify the variables that are used in a file and print those variable names. If I have something like this in a file, /$var/temp_dir/${var2}${var3}.log I want to display the variables 'var', 'var2' and 'var3' from that file. I tried something like... (6 Replies)
Discussion started by: pvamsikr
6 Replies

3. Shell Programming and Scripting

How to identify whether the script is in Unix format or not ?

Hi All, I have the below scenario in my environment Developers used to copy file from windows to Linux box. Some time on the copied file developers miss to run the dos2unix utility. Because of this script gets failed during the execution. Most of the failures are due to the dos2unix format... (7 Replies)
Discussion started by: kalpeer
7 Replies

4. Shell Programming and Scripting

Perl Script to identify files without extension and assign a value

Hi, I have a perl script which is a part of a shell script which read lines from a flat file(which is generated as part of a script after a series of bteq/fexp) and assigns a value for each object in the file based on the type of file name. (i.e extensions like .bteq/.ctl/.ksh etc) For example,... (1 Reply)
Discussion started by: yohasini
1 Replies

5. Shell Programming and Scripting

Script to Identify if folder has a file in it

Hi everyone I am new to the forums. I haven't done much linux myself but I have been asked if I can do the following. Write a linux script that needs to scan a certain folder every x amount of minutes and if there is a file in the folder then it needs to call a different script. Is this... (2 Replies)
Discussion started by: Bosbaba
2 Replies

6. Shell Programming and Scripting

Using an awk script to identify dupes in two files

Hello, I have two files. File1 or the master file contains two columns separated by a delimiter: a=b b=d e=f g=h File 2 which is the file to be processed has only a single column a h c b What I need is an awk script to identify unique names from file 2 which are not found in the... (6 Replies)
Discussion started by: gimley
6 Replies

7. AIX

Script to identify high CPU usage processes

Hi Guys, I need to write a script capable of identifying when a high cpu utilitzation process. It sounds simple but we are on a AIX 5.3 environment with Virtual CPU's (VP's) and logical CPU's. Please any ideas or tips would be highly appreciated. Thanks. Harby. (6 Replies)
Discussion started by: arizah
6 Replies

8. Shell Programming and Scripting

Shell script to identify the number of files and to append data

Hi I am having a question where I have to 1) Identify the number of files in a directory with a specific format and if the count is >1 we need to concatenate those two files into one file and remember that in the second file the header should not be copied. it should be form first file.... (4 Replies)
Discussion started by: pradkumar
4 Replies

9. Shell Programming and Scripting

test script to identify SHELL

I am new to BASH and writing a small script to identify the SHELL . #!/bin/bash BASH='/bin/bash' KSH='/bin/ksh' if then echo "it's Bash" else echo "it's not Bash" fi $ bash -x a.sh + BASH=/bin/bash + KSH=/bin/ksh + '' a.sh: line 4: where am I missing . PLease advice . (10 Replies)
Discussion started by: talashil
10 Replies

10. Shell Programming and Scripting

How to identify the calling script?

Hi, I have two scripts ( /tmp/k1.sh and /tmp/k2.sh ). k1.sh calls the k2.sh . For security reasons, I must be sure that the k2.sh is being called by the k1.sh . Is it possible for the k2.sh identify that it's been called by the k1.sh? I mean, identify the complete path of the k1.sh (... (6 Replies)
Discussion started by: crematoriumm
6 Replies
Login or Register to Ask a Question