Solved

Help needed with bash shell script

Posted on 2013-10-23
7
483 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 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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 …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now