Script to move files and Creation of folder structure

We are receiving few zipped files in one location say : apple/oranges/incoming

All .zip files are placed here in incoming folder.

So few of the files are password encrypted.

There are only 10 zipped files, so we are planning to create a script which will pick that zip file from incoming location and place the files in orange/ongoing.

Before placing the zip files in Ongoing we need to create folders such as 101/801/SAW(we can hard code the folder structure) and then place the file in SAW.
To unzip the files we provide password and then remove the zipped file.

Here is the code I have tried:

#!/bin/sh
root=$(/apples/oranges/Incoming/sx15440101_ope)
for file in $root
do
srcdir=$(/apples/oranges/Ongoing/)
dir=$(mkdir -p 809/116/RAW)
password=$(echo "$file" | awk '{print substr($1, 1, 10)}')
cd "$srcdir" || exit
cp "$file" "$dir"
unzip -P "$password" -0 "$file"
done

Also there are other files which are not password encrypted they just need to be placed in ongoing, create the folder and unzipped.

Here is my environment:

$ uname -a
Linux sosuhenhl01 2.6.32-696.10.1.el6.x86_64 #1 SMP Wed Jul 19 08:18:21 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
$ ps
  PID TTY          TIME CMD
15392 pts/0    00:00:00 ps
41789 pts/0    00:00:00 bash
$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
$ ksh --version
-bash: ksh: command not found
$ sh --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
There is NO WARRANTY, to the extent permitted by law.
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 31  2017 /bin/sh -> bash
$ awk --version
GNU Awk 3.1.7
Copyright (C) 1989, 1991-2009 Free Software Foundation

As the password varies for each file I am trying to individual script for each file which is easy for us to setup the load.

First, you talk about creating a directory called SAW, yet in your code it is RAW. Second, you appear to be extracting the password from the filename. Is that correct? How do you know when a file is encrypted and when it is not?

There is so much wrong with the script as presented I don't know where to begin.

  • Using $() inappropriately
  • mkdir does not print anything do dir is empty
  • How do you know when a file is password protected? Your script appears to expect them all to be
  • Is the password in the filename?
  • You are not putting anything into the ongoing directory.

I'm assuming

root=$(/apples/oranges/Incoming/sx15440101_ope)

is to set root to /apples/oranges/Incoming/sx15440101_ope , but the shell will expect sx15440101_ope to be an executable.
Similarly,

dir=$(mkdir -p 809/116/RAW)

will result in a null value for the variable dir .

I don't know zip so I don't know what this is doing:

unzip -P "$password" -0 "$file"

Are you trying to unzip the file or replace it with a decrypted version?

Andrew