Solved

Help needed with bash shell script

Posted on 2013-10-23
7
496 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 400 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 19

Assisted Solution

by:simon3270
simon3270 earned 100 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 111
UNIX Script. Send email if failure 8 64
Redhat upgrade 1 48
Install XRDP on Ubuntu Server 16.10 x64 3 63
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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