I'm struggling with this script, I have tried different thing but I can't get it to work.
I tried with if ,doesn't work, I tried with nested while also without success. I don't know what to do any more...
If the value imax has reached 10 i want it to start over with 1
this is the code:
folder="f"
ps=20000
name="test"
i=1
imax=1
while [ $i -le 20 ]
do
cd
cd "$folder$((i))"
sed -i 1s/.*/$name$((imax))/ file.txt
sed -i 2s/.*/$ps/ file.txt
(( i++ ))
(( imax++ ))
done
Thank you
Something like this works:
i=1
imax=1
while [ $i -le 20 ]
do
echo $i $imax
(( i++ ))
(( imax++ ))
if [ $imax -eq 10 ]; then imax=1; fi
done
Related
n=20
x=3
count=0
flag=0
i=1
declare -a arr[n+1]
for (( j=0;j<=n;j++ ))
do
arr+=(0)
done
#echo "${arr[#]}"
while [[ $count -ne $n ]]
do
if [[ $i -le $n ]]
then
if [[ ${arr[$i]} -eq '0' ]]
then
echo "Value is ${arr[$i]}"
#${arr[$(i-1)]}= (( ${arr[$i-1]++} ))
${arr[$i]}+=${arr[$i]}
echo " "
#echo -n "${arr[$i]}"
echo -n " $i"
count=$(( count+1 ))
i=$(( i+1+x ))
else
i=$(( i+1 ))
fi
else
i=$(( i-n ))
flag=$(( flag+1 ))
fi
done
echo " "
echo "No of round : $flag"
This is the whole code, I've tried to print numbers that follows this: n=20 is the number of elements and x=3 is the number that we have to avoid. For example,
20
3
1,5,9,13,17,2,6,10,14,18,3,7,11,15,19,4,8,12,16,20,
3
But, the problem is that my second if condition is not fulfilling, if ignores the condition. Above example is for the C++, but in bash script, 2nd if statement isn't working. This can be because syntax is wrong. So can you please help me to find the mistakes.
Output of the above code:
output
${arr[$i]}+=${arr[$i]}
This is incorrect. $ should not be used when you assign the value.
If you want to double the value, replace this string with the following:
arr[$i]=$(( ${arr[$i]} + ${arr[$i]} ))
Or what you want to do there?
I have loop which writes to file, but I want to write each 0.5 value to the file. I tried with let count+=0.5 but that didn't work somehow. Is this possible?
Script:
#!/bin/bash
COUNTER=50
count=0
until [ $COUNTER -lt 20 ]; do
echo $count >> value.txt
echo COUNTER $COUNTER
let COUNTER-=1
let count+=0.5
sleep 1
done
bash doesn't do floating-point arithmetic natively; you need to use an external tool. -= is also not a supported operator.
until [ "$COUNTER" -lt 20 ]; do
printf "%0.1f\n" "$count"
echo "COUNTER $COUNTER"
count=$(bc <<< "$count + 0.5")
COUNTER=$((COUNTER - 1))
sleep 1
done > value.txt
#!/bin/bash
n=1
while (( $n <= 5 ))
do
num$n=`echo "$n"`
n=$(( n+1 ))
done
echo "$num1"
ok so what I am trying to do is create a while loop that will create variables and just put something into it in this case its just the value of n but i cant get it to do this!
so basically it will create num1, num2, num3 and so on
echo "$num1"
echo "$num2"
echo "$num3"
should display
1
2
3
but i keep getting an error am i missing something here cause it shouldnt be anything crazy to do this...
Try with
#!/bin/bash
n=1
while (( $n <= 5 ))
do
eval num$n=`echo "$n"`
n=$(( n+1 ))
done
echo "$num1"
echo "$num2"
echo "$num3"
The problem here is that bash is trying to evaluate num$n as a command, which does not exist, so the error.
Don't dynamically create numbered variable names like this; use an array.
n=1
while (( $n <= 5 )); do
nums[$n]=$n # No need to execute $(echo ...)
n=$((n+1))
done
echo "${num[1]}"
echo "${num[2]}"
echo "${num[3]}"
I tried to do a for loop with 2 conditions but I didn't succeed in any way:
for (( i=0 ; -e /file && i < 10 ; i++ ))
of course I tried any combination of parentheses like:
for (( i=0 ; [ -e /file ] && [ i < 10 ] ; i++ ))
for (( i=0 ; [ -e /file -a i < 10 ] ; i++ ))
What's wrong on this?
I googled a lot for this, but I didn't find any suggestion.
You have to do some subshell trickery to pull this off:
for (( i=0 ; $([ -e /file -a $i -lt 10 ]; echo "$?") == 0; i++ ))
Or probably better:
for (( i=0 ; $([ -e /file ]; echo "$?") == 0 && i < 10; i++ ))
What's happening here is that $(...) is being run and placed into the mathematical expression ... == 0. When it's run the echo "$?" spits out the return code for [ which is 0 for no-error (i.e. expression is true), and 1 for error (i.e. expression is false) which then gets inserted as 0 == 0 or 1 == 0.
Sorry about bits and snippit of information
So I am writing an average shell script program
so if use inputs
echo 1 3, .... | sh get_number
I would have to pull the numbers seperated by spaces from echo to be
var1 = 1, var2= 3, etc.
I tried
#!/bin/sh
sum=0
for i in $*
do
sum=`expr $sum + $i`
done
avg=`expr $sum / $n`
echo Average=$avg
but doesnt work....
do I include a read here?
also how would I do
sh get_number <file1>, <file2>... to grab numbers in them and sum them
in shell script?
Thanks
Sounds like you are looking for the read shell builtin:
% echo "1 2 3 4" | read a b stuff
% echo $b
2
% echo $stuff
3 4
To fix up your code:
for i in $*; do
sum=$(( sum + i ))
n=$(( n + 1 ))
done
echo "Average=$(( sum / n ))"
#!/bin/sh
while [ $# -gt 0 ]; do
(( i++ ))
(( sum += $1 ))
shift
done
echo "Average=$(( sum/i ))"
Note: This fails in dash which is the closest shell I could find to a real sh.
An example of reading values from files passed as command line arguments or from lines read from stdin:
add_to_sum() {
set $*
while [ $# -gt 0 ]; do
I=`expr $I + 1`
SUM=`expr $SUM + $1`
shift
done
}
I=0
SUM=0
if [ $# -gt 0 ]; then
# process any arguments on the command line
while [ $# -gt 0 ]; do
FILE=$1
shift
while read LINE; do
add_to_sum "$LINE"
done < "$FILE"
done
else
# if no arguments on the command line, read from stdin
while read LINE; do
add_to_sum "$LINE"
done
fi
# be sure not to divide by zero
[ $I -gt 0 ] && echo Average=`expr $SUM / $I`