Grouping commands of if...then statement, including assignation of variable for shell/bash in a single line
-1
votes
1
answer
59
views
I know the existence of *while-do-done *. Alternatively, I know that I can use *for-do-done *. But, I was trying to test manually using *if-then-else*.
I would to like to execute a command, but doing a shift with addition in each iteration.
sh-3.2# currPos=0;stepSize=16;
sh-3.2# hexdump -n $stepSize -Cv /dev/disk0s1 -s $currPos;currPos=$(($currPos + $stepSize));echo $currPos;
00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.offset (https://linuxize.com/post/bash-if-else-statement/
something of: Note**: I'm grouping inside of else break):
sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos }; else break; fi
sh: syntax error near unexpected token `else'
sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; }; else break; fi
sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; } else break; fi
sh-3.2#
-s
) is not taking the updated value of variable $currPos
. But in the echo
command the $currPos
variable is changing (16
, 32
and 48
)!
**EDIT 1**: According to *Kusalananda* (Thanks a lot!) I fix the first step.
sh-3.2# currPos=0;stepSize=16;finalStep=48;
sh-3.2# hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos;
00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.if
, then
, else
, fi
and (-eq
, -gt,
-ge
, -lt
, -le
, !=
) and break
.
Trying to use a iterative with if
, break
:
sh-3.2# currPos=0;stepSize=16;finalStep=48;
sh-3.2# if [ $currPos -le $finalStep ]; then (hexdump -n $stepSize -Cv /dev/disk0s1 -s $currPos;currPos=$(($currPos + $stepSize));echo $currPos;); else break; fi
00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.(
and )
the commands (hexdump
, assignation
and echo
) of the then
of the if
statement.
But, in this case, the echo
is working worst the first case (the value of $currPos
variable is not changed).
According to this article I need to use {
and }
. But I get:
sh-3.2# if [ $currPos -le $finalStep ]; then {hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos;}; else break; fi
sh: syntax error near unexpected token `}'
**How I can do that?, How solve this (the update value of variable is not taken)?**
**EDIT 2** *Stéphane-Chazelas* Helped me! (read comments about {
and
}
, note the space after of {
and before of }
respectively).
sh-3.2# currPos=0;stepSize=16;finalStep=48;
sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; }; else break; fi
00000000 8d 1c 09 48 65 8c 3c 6e 01 00 00 00 00 00 00 00 |...He.
Asked by joseluisbz
(375 rep)
Aug 15, 2022, 05:58 AM
Last activity: Aug 15, 2022, 08:02 AM
Last activity: Aug 15, 2022, 08:02 AM