Solved

Help needed with bash shell script

Posted on 2013-10-23
7
501 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

718 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