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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Help with UNIX / Linux

Im reading a book to help me learn UNIX. Im trying an example and Im stuck.  Any help would be greatly appreciated!

Im not even sure where to begin. The book confused me a bit.


The script needs to perform the following pseudocode in the order shown:

Set a variable named "x" to 10
Set a variable named "y" to 100
Display the result of dividing "y" by "x" and place the result into variable "t"
Display the result of dividing "y" by 0
Display the result of multiplying "y" by "x" and place the result into variable "t"
Display the result of adding "y" and "x" and place the result into variable "t"
Display the result of subtracting "x" from "y" and place the result into variable "t"
Display the result of raising "y" to the power of 2 and place the result into variable "t"
Display the result of taking the remainder of "y" divided by "x" and place the result into variable "t"

Execute the script, and then record the commands used.
0
Cyber IT
Asked:
Cyber IT
  • 6
  • 4
  • 2
  • +1
3 Solutions
 
WizRd-LinuxCommented:
Have you even started the bash script?  Are you able to post what you have already rather than simply handing you the answer?

I personally would prefer to help you get to a solution rather than just have the answer.
0
 
Cyber ITEngineerAuthor Commented:
I wasnt looking for an answer. I was looking for help to start it off.  Thanks!
0
 
Cyber ITEngineerAuthor Commented:
Im working on something now... will post more when I can. Thanks!
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
WizRd-LinuxCommented:
Okay, the best advice I can give you is to set a variable, you just set it.

x=1
variable=10

To reference the variable through the script, use ${variable} or ${x}.

Check out http://tldp.org/LDP/abs/html/.  This contains ALL of the functions and operations you will need to write the script.
0
 
Cyber ITEngineerAuthor Commented:
This is what I got going.

((x = 10))
((y = 100))

((y/x = t))
echo $t

((y/0))
echo $y

((y*x = t))
echo $t

((y+x = t))
echo $t

((x-y = t)
echo $t

((y**2 = t))
echo $t

((y%x = t))
echo $t
0
 
ozoCommented:
Did you say that you were doing this in bash?
Of not, please  specify in which shell or other language you are writing this script
If you are doing it in bash
you can type
man bash
to get started
0
 
ewest02Commented:
First line of a script will typically begin with

#!/bin/sh, #!/bin/ksh, #!/bin/bash   etc... This first line indicates to the shell which program will actual be passed the contents of the script. The command is immediately preceded by "#!" ..

...so

#!/bin/sh

x=10
y=100
echo "x=${x}, y=${y}"

Initialize x and y and then display their values. Strictly speaking you do not need the curly braces here, but it is a good habit to get into...  Refer to the text that describes variable expansion.

From the syntax you have posted it appears that you are either using bash or ksh.  Note that the left side of the equals "=" or lval is assigned the resulting value of the right side or rval.

((t=x+y))

assigns the result of the addition of the values of x and y to t

Good luck.

  --Eric
0
 
Cyber ITEngineerAuthor Commented:
I created the following:

#!/bin/bash
((x = 10))
((y = 100))
((t = y / x))
echo $t
((y / 0))
((t = y*x))
echo $t
((t = y+x))
echo $t
((t = y - x))
echo $t
((t = y**2))
echo $t
((t = y % x))
echo $t

then I went to my working directory in KNOPPIX and ran the script by entering the name of the file in the shell - konsole.

0
 
WizRd-LinuxCommented:
Display the result of dividing "y" by 0 - Btw, you can't divide by zero, bash will spit the dummy.

The rest of your script should work perfectly fine.

0
 
ozoCommented:
So what happened when you ran the script by entering the name of the file in the shell ?
0
 
Cyber ITEngineerAuthor Commented:
WizRd-Linux:

I know what you mean but I did what the directions said to do in the question.  Am I missing something?

Thanks!
0
 
Cyber ITEngineerAuthor Commented:
ozo:

The following is the output that I received after running the script:

10
./name.3-7: line 6: ((: y/0: division by 0 (error token is "0")
1000
110
90
10000
0
0
 
WizRd-LinuxCommented:
See the division by 0 :)

Linux will not do this, it is something you would have to "trap" and report a message accordingly, it is a little hard without the 0 being a variable which you can test for.

I would be confident to say that you could move on without missing the point of what you were trying to achieve
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now