Saving Mod in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Saving Mod in a variable
# 8  
Old 11-27-2017
This might be a suitable script to do what the comments in your code say it is trying to do:
Code:
#!/bin/bash
echo "This is a shell script to remove numbers divisible by 2 from an array"
if [ $# -gt 0 ]
then	# Use commnand line arguments as array elements.
	arr=($@)
else	# Use default array elements.
	arr=(1 6 9 12 15)
fi
case ${#arr[@]} in
(0)	echo 'Original array is empty.';;
(1)	echo "Original array element is ${arr[@]}.";;
(*)	echo "Original array elements are ${arr[@]}.";;
esac

# For each element in the array...
for (( i=0; i<${#arr[@]};  ))
do
	# ... determine if that element is evenly divisible by 2.
	if ((arr[i] % 2))
	then
		# Element i is not evenly divisible.  Evaluate next element.
		echo "Number (arr[$i]=${arr[i]}) is not divisible"
		((i++))
	else
		# Element i is evenly divisible; remove it from the array and
		# re-evaluate element i.
		echo "Number (arr[$i]=${arr[i]}) is divisible"
		arr=(${arr[@]:0:i} ${arr[@]:i+1})
		echo "Modified array is ${arr[@]}."
	fi
done
case ${#arr[@]} in
(0)	echo 'Final array is empty.';;
(1)	echo "Final array element is ${arr[@]}.";;
(*)	echo "Final array elements are ${arr[@]}.";;
esac

Here are a few of the reasons for changes made to your code:
  1. There is no need to invoke bc to perform mod operations since arithmetic expansions in the shell can do that just as well as long as the values in your array are not larger than values that can be assigned to a long integer in C.
  2. When an array element is removed from your array, you need to keep i (not i-1) elements of the array before ${arr[i]} is removed. Note that array elements are numbered starting with 0; not starting with 1.
  3. When array element i is removed from your array, you can't increment i. If you do increment i after removing an element, the new arr[i] won't be checked to see if it is divisible by 2.
  4. The errors you were getting are because your loop end check is based on the original size of your array, not the array size after elements are removed from your array. Therefor, the last two times you invoke bc you are invoking it with non-existent elements of your array as an argument to be processed.
  5. Taking out the declare statement makes the code executable by both bash and ksh.
This User Gave Thanks to Don Cragun For This Post:
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