shell script(calculate the division of two variables taking from a file) [duplicate] - shell

This question already has answers here:
How do I use floating-point arithmetic in bash?
(23 answers)
Closed 6 years ago.
I would like to divide two numbers which were extracted from a file using the below commands
temp1= grep PERM_ALLOCATED_SIZE /log/health_eg/DBsize.txt | cut -d':' -f2 | tr -d ' '
temp2= grep PERM_IN_USE_SIZE /log/health_eg/DBsize.txt | cut -d':' -f2 | tr -d ' '
and I'm able to print this
251658240
16239740
temp1 and temp2 respectively but I could not able to perform division for the above..
Sample output:
temp=temp2/temp1(0.064)

A possible solution is using echo and bc -l:
echo "251658240/16239740" | bc -l
Output:
15.49644514013155383029
Using your example, you can do that:
temp=`echo $temp2/$temp1*0.64 | bc -l`

Related

Argument in bash script

I have the following bash script called countscript.sh
1 #!/bin/bash
2 echo "Running" $0
3 tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed $1 q
But I don't understand how to pass the argument correctly: ( "3" should be the argument $1 of sed).
$ echo " one two two three three three" | ./countscript.sh 3
Running ./countscript.sh
sed: -e expression #1, char 1: missing command
This works fine:
$ echo "one two three four one one four" | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed 3q
3 one
2 four
1 two
Thanks.
PS: Anybody else noticed the
bug in this script on page 10, https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-2.pdf ?
In the quoted paper, I think you are misreading
sed ${1}q
as
sed ${1} q
and sed does not consider 3 by itself a valid command. The separate argument q is treated as an input file name. If the value of $1 did result in a single valid sed script, you would have likely gotten an error for the missing input file q.
Proper shell programming would dictate this be written as
sed "${1}q"
or
sed "${1} q"
instead; with the space as part of the script, sed correctly outputs the first $1 lines of input and exits.
It's somewhat curious that the authors used sed instead of head - "$1" to output the first few lines, as one of them (McIlroy) essentially invented the idea of the Unix pipeline as a series of special-purpose, narrowly focused tools. Not having read the full paper, I don't know what Knuth and McIlroy's contributions to the paper were; perhaps Bentley just likes sed. :)
When running the following command:
$ echo " one two two three three three" | ./countscript.sh 3
the special variable $1 will be replaced by 3, your first argument. Hence, the script runs:
tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed 3 q
Notice the space between the 3 and the q. sed does not know what to do, because you give it no command (3 is not a command).
Remove the space, and you should be fine.
tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed "${1}q"

working with "unclear" declared variables [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I am trying to save the specific output from a piped command to a variable.
value= ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"
This saves the average ping to the variable "value".
However when I try to further process the variable e.g. in an echo line like:
echo "The Average ping is: $variable"
The output is
The Average ping is: $variable
Even when i try to pass the value to another Variable like:
value2= $value
the result is the same.
I read that variables in bash need to be declared in a certain way, may this be the problem in this specific case?
sh or bash:
value="`ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r"`"
bash:
value="$(ping google.de -c 20 | grep -oe \/[0-9]. | head -n 1 | tr -d [\/] | tr -d "\n\r")"

Assigning a command output to a shell script variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
How do I assign a command output to a shell script variable.
echo ${b%?} | rev | cut -d'/' -f 1 | rev
${b%?} gives me a path..for example: /home/home1
The above command gives me home1 as the output. I need to assign this output to a shell script variable.
I tried the below code
c=${b%?} |rev | cut -d '/' -f 1 | rev
echo $c
But it didn't work.
To assign output of some command to a variable you need to use command substitution :
variable=$(command)
For your case:
c=$(echo {b%?} |rev | cut -d '/' -f 1 | rev)
Just wondering why dont you try
basename ${b}
Or just
echo ${b##*/}
home1
If you want to trim last number from your path than:
b="/home/home1"
echo $b
/home/home1
b=${b//[[:digit:]]/}
c=$(echo ${b##*/})
echo ${c}
home
Just like this:
variable=`command`

How to capture the output of curl to variable in bash [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
So, lets say I have the following command:
curl -I http://google.com | head -n 1| cut -d $' ' -f2
This will capture the http status code??
Now I want to assign this to variable.. in bash script
like output = "curl -I http://localhost:8088/tracks?key=9 | head -n 1| cut -d $' ' -f2"
or something like that..
How do I assign the response of above command to a variable called output in bash?
Thanks
You have two options (see this StackOverflow answer here):
Preferred: Surround the invocation in $()
Surround the invocation in back ticks
NOTE: back ticks are legacy, the former method is preferred.
output=$(curl -I http://google.com | head -n 1| cut -d $' ' -f2)
echo "$output";
output=`curl -I http://google.com | head -n 1| cut -d $' ' -f2`
echo "$output";

Shell script cut command [duplicate]

This question already has an answer here:
Shell script, saving the command value to a variable
(1 answer)
Closed 9 years ago.
I am trying to cut ABCservice and DEFService dfrom the array and print them.What am I missing here?
urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
for url in "${urlArray[#]}"
do
service=echo $url|cut -f4 -d/
echo $service
done
Expected Output:
ABCService
DEFService
Current Output:
./test1.sh: line 6: http://server:port/ABCservice/services/ABCservice?wsdl: No such file or directory
./test1.sh: line 6: http://server:port/DEFservice/services/DEFservice?wsdl: No such file or directory
What abut this?
service=$(echo $url | cut -d"/" -f4)
echo $service
or directly
echo $(echo $url | cut -d"/" -f4)
The problems in your code:
service=echo $url|cut -f4 -d\
to save a command output in a variable, we do it like this: service=$(command).
your cut had \ as delimiter instead of /. Also it is good to wrap it with brackets: -d "/"
service=$(echo $url | cut -d/ -f6 | cut -d\? -f1)
echo $service
Using bash string function:
for i in "${!urlArray[#]}"; do
urlArray[i]="${urlArray[i]%\?*}"
urlArray[i]="${urlArray[i]##*/}"
echo ${urlArray[i]}
done
$ urlArray=('http://server:port/ABCservice/services/ABCservice?wsdl' 'http://server:port/DEFservice/services/DEFservice?wsdl')
$ for i in "${!urlArray[#]}"; do urlArray[i]="${urlArray[i]%\?*}"; urlArray[i]="${urlArray[i]##*/}"; echo ${urlArray[i]} ; done
ABCservice
DEFservice
Your delimiter should be a forward slash
echo 'http://server:port/ABCservice/services/ABCservice?wsdl' | cut -f 4 -d/
The \ is an escape character. Try \\ instead.

Resources