?
Solved

Help needed with bash shell script

Posted on 2013-10-23
7
Medium Priority
?
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

650 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