Saving Mod in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Saving Mod in a variable
# 1  
Old 10-06-2017
Saving Mod in a variable

Hello Experts,
In one of my shell script, I've been trying to calculate mod and saving it in a variable, below is what I have tried but it isn't working.
Any help appreciated!!!
Code:
#!/bin/bash
num1=4
num2=3
echo "Number one is $num1"
echo "Number two is $num2"
mod_final=$(( echo "num1%num2" | bc ))
echo "Final Mod is $mod_final"

Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums

Last edited by RudiC; 10-06-2017 at 02:02 PM..
# 2  
Old 10-06-2017
You don't need bc's help to do this.
Code:
num1=4
num2=3
mod_final=$(( num1%num2 ))

# 3  
Old 10-06-2017
You are
- using "Arithmetic Expansion" in lieu of "Command Substitution"
- NOT expandig the variables num1 and num2
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-06-2017
And if you *want* bc, maybe for handling very long numbers, then it is
Code:
num1=12345678901234567890
num2=46
mod_final=$( echo "$num1%$num2" | bc )

=>Command substitution.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 11-26-2017
Hello Experts,

How can i do this with array, this is what I am trying :
Code:
num=$( echo "${arr[i]}%2" | bc )

# 6  
Old 11-26-2017
Quote:
Originally Posted by mukulverma2408
. . .
How can i do this with array,
. . .
If we only knew WHAT you want to do?
# 7  
Old 11-26-2017
Quote:
Originally Posted by RudiC
If we only knew WHAT you want to do?
Hello RuciC,

Apologies for not being clear enough, I have been trying to create script that will remove number divisible by 2 from array, this is what I've tried :
Code:
#!/bin/bash
echo "This is a shell script to remove number divisible by 2 from the array"
declare -a arr=(1 6 9 12 15)
echo "Original array is ${arr[@]}"
for (( i=0; i<5; i++ ))
do
   num=$( echo "${arr[i]}%2" | bc )
   if [[ $num -eq 0 ]]
   then
       arr=(${arr[@]:0:(( $i-1))} ${arr[@]: (( $i+1 )) })
       #echo "Number divisible"
   fi
done
echo "Modified array is ${arr[@]}"

Error being encountered is :
Code:
(standard_in) 1: syntax error
(standard_in) 1: syntax error

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - saving results of external script to variable.

So, I've been playing with speeding up some analysis we do by using multiple threads of awk (actually, mawk, but code-compatible as far as I use it) on multiple CPU cores. So, I have a big data file and I have several copies of exactly the same processor script, written in mawk. I also have a... (8 Replies)
Discussion started by: treesloth
8 Replies

2. Shell Programming and Scripting

Unwanted execution instead of saving to environment variable

My .bash_profile file: source $HOME/build.variables My build.variables file: export threads="$(${threads:-$(lscpu | egrep "CPU\(s\):|Thread" | $]\)\n. *Thread.*\(]\)@\1*\2@;Ta")])})"threads=$(lscpu | egrep "CPU\(s\):|Thread" | $]\)\n.*Thread.*\(]\)@\1*\2@;Ta")The above codes when not comment out... (7 Replies)
Discussion started by: charlieandlinux
7 Replies

3. Shell Programming and Scripting

Non trivial file splitting, saving with variable filename

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have a few thousands text files (books). Each book has many chapters. Each chapter is identified by a cite-key. We need to split each of those book files by... (4 Replies)
Discussion started by: samask
4 Replies

4. Shell Programming and Scripting

shell script for saving oracle database records in variable

i want to retrieve value in each column of each row in sql plus and save them into array variable and echo the value in array variable (2 Replies)
Discussion started by: ramish
2 Replies

5. Shell Programming and Scripting

Trouble saving variable

Hi, I have problems when you save a variable of a command. I have put the following line: CONEXION_BAGDAD = $ (grep-c "Please login with USER and PASS" $ LOG_FILE_BAGDAD) But I returned the following error: syntax error at line 67: `CONEXION_BAGDAD = $ 'unexpected Because it can happen?... (2 Replies)
Discussion started by: danietepa
2 Replies

6. Shell Programming and Scripting

Help with capturing homedir via ssh and saving to variable

I need to capture the homedir using the ssh command and then saving it to a variable. The results from the following command is what I need to capture to a variable: NOTE: the value I'm getting back is also incorrect. as it seems to be getting the home dir from the local server and not the... (2 Replies)
Discussion started by: reneuend
2 Replies

7. UNIX for Dummies Questions & Answers

saving command output to a variable

Hello, I have a shell script containing a command string in the following format: command1 | command2 | cut -c9-16 The output from this is a record number (using characters 9-16 of the original output string) e.g. ORD-1234 I wish to save this value to a variable for use in later commands... (4 Replies)
Discussion started by: philjo
4 Replies

8. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Saving output from awk into a perl variable

How would I pass awk output to a perl variable? For example, I want to save the value in the 4th column into the variable called test. My best guess is something as follow, but I am sure this isn't correct. $test = system("awk '/NUMBER/{print \$4}' $_"); (8 Replies)
Discussion started by: userix
8 Replies
Login or Register to Ask a Question