Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
Write a bash shell script that takes as an argument a name. Then your script will ask the user for two numbers. It should then display the name and the sum (addition) and product (multiplication) of the two numbers
I don't know how to get numbers and add them
2. Relevant commands, code, scripts, algorithms:
echo Please, enter your name
3. The attempts at a solution (include all code and scripts):
#!/bin/bash
echo Please, enter your name
read NAME
echo "$NAME!"
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Sheridan, Brampton Canada, SYST13416 LinuxUNIXOperating Systems
Martin
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
Well, numbers often come from string data, for instance if first argument on the command line, "$1". You can read text in from the keyboard and pattern-test it to ensure it is valid text. Bash and ksh support $(( )), which does all sorts of integer arithmetic, e.g., z=$(( $z + 1 )), and if (( )), which allows all sorts of integer tests, e.g., if (( $z < 30 )). I am not sure they have a friendly floating point, probably. Let me google a bit . . . no, bash does not. I use dc or bc to do my floating point calculations within scripts, or move to a more formal language. The text from bc can be tested to see if it is negative, for instance.
But then it might not be ksh portable![CODE]$ echo "(( 1 + 2 ))"
(( 1 + 2 ))
$ echo (( 1 + 2 ))
ksh: syntax error: `((' unexpected
That's not "unportable" -- that's simply wrong. BASH and KSH alike agree this is a syntax error, and for good reason:
Produces a string: $((stuff))
Produces no string: ((stuff))
Plain (( )) without the $ is more like a [ ] statement. Try it:
When you use them in a syntactically valid manner, as their own statement, they're fine: ((k = k + 5))
You also missed my point about k vs $k inside (( )). ((k=k+3)) works, (($k=$k+3)) does not since K gets substituted before anything starts calculating, so you end up trying to assign the value of 3 or something. Just plain variable names don't ever need a $ inside a (( )) expression anyway, why make them more complicated?
Last edited by Corona688; 04-12-2011 at 04:08 PM..
I do not know shell scripting. But at work place, I have got an in and out shell scripting task. I just need to understand a very big script. Is there any tool in which I can place the script and it can tell me the meaning of the whole script? (3 Replies)
I'm having trouble trying to create a BASH shell script.
I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit.
So far I have this:
#!/bin/bash
read "cat file" // User will input command cat... (7 Replies)
Hello All,
I am setting up a cron job, where i am calling a shell script to make few builds. I got struck at a point, need some expert inputs to proceed further.
The script is categorized in 5 parts and in the last part while building software it asks for few questions like:-
1. Build mode... (4 Replies)
i am new to shell scripting and stuck at one place in my program.
i am reading data from one structured file and extracting some data from particular lines and then writing into the output file. In that reading input file line by line from while loop.
while read line
do
rectype=line... (7 Replies)
#! /bin/sh
abcd = "Hello world"
if
then
echo $abcd
fi
i got error message that
line3 : abcd: command not found
line5 : [0: command not found
line5 : [1: command not found
i have no idea why i got this message. Can some one help me ??? (6 Replies)
Hi,
Actually i have written one test.sh (shell program) in bash.
Here i have a variables $a which stored the value package1.
Now I want to write a regular expression inside the if command that "if $a variable contains letter p in the begining of the value package1 then it is coming true.... (5 Replies)
Can any1 please help me...
i'm really lost in using bash shell scripting...
and i got to hand this up on monday...
please anyone teach me how to do this assignment...
Please use basic things because i just learn the program only... thanks
... (1 Reply)
Hi,
i am new to UNIX. I have couple of basic questions.
1. Is the syntax for BASH shell programming same in the LINUX and SUN SOLARIS operating systems?
2. I have to work on BASH shell programming in SUN SOLARIS operating system. I am going through the documentation from the following... (4 Replies)