[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help needed with bash shell script

Posted on 2013-10-23
7
Medium Priority
?
509 Views
Last Modified: 2013-10-23
Can anyone explain the operations done by the shell script line-by-line.

if
[ $# = 3 ]
then
  mkdir -p $1/$2
  mkdir -p $1/../done/$2
  sed -e s/\$dir/$2/  < student.tmp > $1/student.ftp
else
  sed -e s/\$dir//  < student.tmp > $1/student.ftp
fi
cd $1
/usr/bin/ftp -in < student.ftp > student.out 2>&1 

Open in new window

0
Comment
Question by:gaugeta
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1600 total points
ID: 39593725
1) if
2) the number of arguments ($#) is exaxtly three
---> Below I'll assume the arguments to be "a", "b" and "c"
3) then
4) create a directory path below the current one and name it according to the first two arguments. Example: "mkdir a/b"
5) create another directory path using the first two arguments, example: "mkdir a/../done/b" which is exactly the same as "mkdir done/b" (see below).
--> Attention: "$1/../" will have no effect because it reverses itself.
6) use "sed" to read the file "student.tmp" from the current directory, change the string (!) "$dir" to what's in argument 2  (Example: "$dir" becomes "b") and create a new file "student.ftp" below the first level of the just newly created path (Example: "> a/student.ftp")
7) else (i.e. if the number of arguments is not three):
8) use "sed" to read the file "student.tmp" from the current directory, change the string (!) "$dir" to nothing (i.e. remove it) and create a new file "student.ftp" below the first level of the just newly created path (see below!) (Example: "> a/student.ftp")
---> Attention: If we come here this directory path has not been created, because we didn't process the "then" branch!
9) fi ( = end of "if")
10) change to the first level of the just newly created path (Example: "cd a"
---> Attention: This directory path will not have been created if the script wasn't run with exactly three arguments (see the previous remark)!
11) Start an FTP session using the just created file "student.ftp" to read the FTP commands from, write the standard output created by the FTP session to a new or overwritten file"student.out" in the current directory ("a") and redirect the error messages to that same file. ("2>&1").
---> Remark: The third argument is never used, although the "then" branch is only executed if three arguments are given.
0
 
LVL 20

Assisted Solution

by:simon3270
simon3270 earned 400 total points
ID: 39593773
Couple of mods to this explanation:
- The script seems to assume that $1 is some common directory, so exists before the call is made - the "mkdir" calls just make $2 inside $1, and done/$2 one level up from $1.
- The "$1/../" is needed if $1 is a multilevel path, e.g. /var/log/ftplogs/new
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39593789
@simon3270:

>> ... that $1 is some common directory, so exists ... <<
Maybe, but if that's the case the script should imperatively test for the directory's existence!

>> The "$1/../" is needed if $1 is a multilevel path ... <<
Correct. Good point!
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 

Author Comment

by:gaugeta
ID: 39593857
@woolmilkporc & @simon3270: Thanks for the replies.

"The "$1/../" is needed if $1 is a multilevel path" Can you please explain this statement.

I thought
mkdir -p $1/../done/$2

Open in new window

would have no effect as it reverses itself as woolmilkporc initially mentioned.

BTW yes $1 is assumed to exist and its a multilevel path, but I did no get why "$1/../" is required.
0
 
LVL 5

Expert Comment

by:Dave Gould
ID: 39593877
Imagine you have $1 = /a/b/c/d/e
then "$1/../done" will take you to /a/b/c/d/done

that is why $1 is still necessary - it was used to provide the /a/b/c/d part.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39593887
Taking simon3270's example:

If $1 is "/var/log/ftplogs/new" and if $2 is "b" then, assuming that your current directory is "/home/gaugeta,

mkdir -p $1/../done/$2

would resolve to

mkdir -p /var/log/ftplogs/done/b

but

mkdir -p done/$2

would resolve to

mkdir -p /home/gaugeta/done/b


Under my initial assumption ($1 is single level and doesn't yet exist), then, assuming that $1 is "a" and $2 is "b" (your current directory is still "/home/gaugeta"),

mkdir -p $1/../done/$2

would resolve to

mkdir -p a/../done/b

which is the same as

mkdir -p done/b

thus creating

/home/gaugeta/done/b

in either form.
0
 

Author Comment

by:gaugeta
ID: 39593909
@all : Thanks a lot for the answers.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question