2 statements in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 2 statements in for loop
# 1  
Old 07-28-2016
2 statements in for loop

Bash shell, variables i and rem are working fine in 2 separate for loops, but I'd like to consolidate them like this:
Code:
for  [ i in $((x*x)) ]  && [ rem in $((((i % 2)) | bc)) ]

This gives syntax error on &&.
Thanks in advance for direction.

Last edited by p1ne; 07-28-2016 at 03:50 PM.. Reason: no parse
# 2  
Old 07-28-2016
I'd be surprised if the error occurs when && is encountered, as none of the two bash for- loop syntaxes are met.

What exactly are you after?
# 3  
Old 07-28-2016
Thanks for reply. Here is the working script. You can see commented section referenced in first post; I'd like to consolidate:
Code:
#!/bin/sh

x=0

echo 'Enter number:'
read x
for  i in $((x*x))  #&& [ rem in $((((i % 2)) | bc)) ] 
do
echo "Square of $x is $i."
done

for rem in $((((i % 2)) | bc))
do
if [ $((i % 2)) -eq 0 ]; then
    echo "$i is even, because the remainder of $i divided by 2 is 0."
else
    echo "$i is odd, because the remainder of $i divided by 2 is $rem, not 0."

fi
done

# 4  
Old 07-28-2016
The for construct is used to iterate through a list or repeatedly evaluate an expression. Using it for one single value is at least, hmmm, questionable, as the loop variable is set to exactly that single value.


Code:
x=0
echo 'Enter number:'
read x
i=$((x*x))  #&& [ rem in $((((i % 2)) | bc)) ]   # integer arithmetics.
echo "Square of $x is $i."
rem=$((i % 2))                                   # bash can do integer arithmetics only, so NO bc needed...
if [ $rem -eq 0 ]
  then echo "$i is even, because the remainder of $i divided by 2 is 0."
  else echo "$i is odd, because the remainder of $i divided by 2 is $rem, not 0."
fi

This User Gave Thanks to RudiC For This Post:
# 5  
Old 07-28-2016
Hello p1ne,
I would like to help you to make good scripts. But to do this I want you to think about the following questions:
  1. what is the use of "for loops"? Why would one use them?
  2. to how many values will $((x*x)) be evaluated?
  3. would the chosen construction be the best way to reach your goal?
# 6  
Old 07-29-2016
Quote:
Originally Posted by RudiC
The for construct is used to iterate through a list or repeatedly evaluate an expression. Using it for one single value is at least, hmmm, questionable, as the loop variable is set to exactly that single value.

Thanks again RudiC, I understand now that loop is questionable for single value.

Code:
x=0
echo 'Enter number:'
read x
i=$((x*x))  #&& [ rem in $((((i % 2)) | bc)) ]   # integer arithmetics.
echo "Square of $x is $i."
rem=$((i % 2))                                   # bash can do integer arithmetics only, so NO bc needed...
if [ $rem -eq 0 ]
  then echo "$i is even, because the remainder of $i divided by 2 is 0."
  else echo "$i is odd, because the remainder of $i divided by 2 is $rem, not 0."
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop statements order of operations

Say I have a for loop that parse through a file....Say it look for the colors red and blue in sections of the file. Say it find red before it find blue in the file. Say I have two if statements in the for loop Something like if blue is found print blue is my favorite color is the first if... (7 Replies)
Discussion started by: scj2012
7 Replies

2. Shell Programming and Scripting

Too many if statements..

Hello. I am new here and new to scripting. I used to have a very basic script that worked for simple backup/restore of files. I have expanded it and well... I have ended up with a complete mess. It still backs up and restores but there is so many issues that stem from the many if statements I... (3 Replies)
Discussion started by: gameinn
3 Replies

3. Homework & Coursework Questions

Using While and If statements

1. The problem statement, all variables and given/known data: Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while... (11 Replies)
Discussion started by: jjb1989
11 Replies

4. Shell Programming and Scripting

How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus, I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script.... How can I do this..... (7 Replies)
Discussion started by: ustechie
7 Replies

5. UNIX for Dummies Questions & Answers

Help with For Statements

Hi, I am trying to write a for statement that will allow for the ps, who, finger, and date commands to run. Can anyone help? I use Putty. (22 Replies)
Discussion started by: lexydoll87
22 Replies

6. Shell Programming and Scripting

HELP!! if statements

I am kind of new in Unix and i have to make a menu. I want to put an if statement in the menu. you should enter the filename and it goes to that file. How do i do this? (1 Reply)
Discussion started by: trob
1 Replies

7. Shell Programming and Scripting

is that possible to keep statements in any loop??

Hi, Actually i stored all validdisks in one array and corresponding partitions required for all individual disks in other array.. Example: Validdisks=dsk2 dsk3 dsk5 ValidPartition=4 4 3 Now i have to create domain.. Domain creation can be done by below commands: fs_setup -d... (1 Reply)
Discussion started by: mansa
1 Replies

8. Shell Programming and Scripting

for i loop with conditional statements?

New to scripting in general, so patience plz. If I ask a stupid question or don't get it, I thank you for your kindness in advance. That said, did a for i loops checks to see if a PB* file is there but I need to know two things before I copy the file. I need to know if the file's create date... (2 Replies)
Discussion started by: xgringo
2 Replies

9. Shell Programming and Scripting

Please help on IF statements.

I had different problem scenarios with IF statement. Can any expert please enlighten me on the difference with these scenarios. Thank you. 1st Scenario: testdate=`date +%Y%m` test=`cat /var/log/database0.$testdate*.log | grep "Errors found during processing" | tail -10` if then ... (4 Replies)
Discussion started by: filthymonk
4 Replies

10. Shell Programming and Scripting

or statements?

how do i do an or in an if-then statement? i tried: if ; then bleh fi how???? (1 Reply)
Discussion started by: Blip
1 Replies
Login or Register to Ask a Question

Featured Tech Videos