0

I want a script that changes directory to the directory where the file.sh is located, lets say var1.
Then I want to copy files from another location ,lets say var2, to the current dir which would be var.
Then I want to do some unzipping and deleting rows in the files, which would be in var

I have tried the below, but my syntax is not correct. Can someone please advise?

#!/bin/bash
# Configure bash so the script will exit if a command fails.
set -e 

#var is where the script is stored
var="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd";
#another dir I want to copy from
var2 = another/directory

#cd to the directory I want to copy my files to 
cd "$var" + /PointB  

#copy from var2 to the current location
#include the subdirectories 
cp -r var2 .

# This will unzip all .zip files in this dir and all subdirectories under this one.
# -o is required to overwrite everything that is in there
find -iname '*.zip' -execdir unzip -o {} \;

#delete specific rows 1-6 and the last one from the csv file
find ./ -iname '*.csv' -exec sed -i '1,6d;$ d' '{}' ';'
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98
  • 1
    Possible duplicate of [Can a Bash script tell what directory it's stored in?](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) If a script can tell which directory it is in, changing to that directory is trivial, of course. – Jonathan Leffler Feb 05 '14 at 01:12
  • 1
    Don't use + to concatenate strings. Don't use spaces around the = signs. – that other guy Feb 05 '14 at 01:15
  • This smells like an online exam. Notice how the 2 `find`s differ slightly in syntax? Notice how the vars are used differently in all lines? – alvits Feb 05 '14 at 02:57
  • it's not for an exam. my 2 find commands work fine as is but could possibly be written in a better/more correct way.the trouble i'm having is with the cd and cp lines – HattrickNZ Feb 06 '14 at 17:56

1 Answers1

1

a few mistakes here:

# no: var="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd";
var=$(cd "$(dirname "$0") && pwd)

The stuff in $() executes in a subshell, so the "pwd" must be performed in the same shell that you have "cd"-ed in.

# no: var2 = another/directory
var2=another/directory

The = cannot have whitespace around it.

# no: cd "$var" + /PointB  
cd "$var"/PointB  

shell is not javascript, string contatenation does not have a separate operator

# no: cp -r var2 .
cp -r "$var2" .

Need the $ to get the variable's value.

# no: find -iname '*.zip' -execdir unzip -o {} \;
find . -iname '*.zip' -execdir unzip -o {} \;

Specify the starting directory as the first argument to find.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • initially this did not work for me, I'll have another look. my first declaration was working so not sure why i need to change it. and as I said above the trouble i'm having is with the cd and cp lines.tks – HattrickNZ Feb 06 '14 at 17:59
  • You must have mis-typed your code: you have two open `$(` but only one close `)` – glenn jackman Feb 06 '14 at 18:21
  • there is 3 `"` in the `var` declaration maybe 1 is missing? – HattrickNZ Feb 06 '14 at 20:35