私が知っている存在完全。または、次のものが利用可能であることを知っています。やることを完了。しかし、以下を使用して手動でテストしようとしました。if-then-else。
コマンドを実行したいのですが、繰り返しごとに追加の変換を実行したいです。
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.<n........|
00000010
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.<n........|
00000010
32
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.<n........|
00000010
48
sh-3.2#
ご覧のとおり、 offset
(-s
)は変数の更新値を使用しません$currPos
。ただし、echo
コマンドでは$currPos
変数が変更されます(16
、32
および48
)!
編集1:によると先行は達成するのが難しい(本当にありがとうございます!) 最初のステップを修正しました。
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.<n........|
00000010
16
sh-3.2# hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos;
00000010 bd 04 00 00 00 00 00 00 01 00 00 80 00 00 00 00 |................|
00000020
32
sh-3.2# hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos;
00000020 4e 58 53 42 00 10 00 00 00 f4 01 00 00 00 00 00 |NXSB............|
00000030
48
以下を試してみましょう。https://linuxize.com/post/bash-if-else-statement/
いくつか:if
、then
、else
、fi
(-eq
、-gt,
-ge
、-lt
、-le
、!=
)とbreak
。
繰り返しを試してください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.<n........|
00000010
16
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.<n........|
00000010
16
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.<n........|
00000010
16
Note
:ステートメントのコマンド(、および)をグループ化します(
。)
hexdump
assignation
echo
then
if
ただし、この場合、最初のecho
ケースは最も悪く動作します(変数の値は$currPos
変更されません)。
これによると記事{
とを使用する必要があります}
。しかし、私は次のようになります。
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 `}'
どうすればいいですか? 、(変数の更新値を使用せずに)この問題を解決するには?
編集2 スティーブン・チャジェラス私を助けた! ({
およびの説明を読んで、それぞれofの後ろと前にスペースを書き留めてください }
)。{
}
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.<n........|
00000010
16
sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; }; else break; fi
00000010 bd 04 00 00 00 00 00 00 01 00 00 80 00 00 00 00 |................|
00000020
32
sh-3.2# if [ $currPos -le $finalStep ]; then { hexdump -n $stepSize -Cv -s $currPos /dev/disk0s1;currPos=$(($currPos + $stepSize));echo $currPos; }; else break; fi
00000020 4e 58 53 42 00 10 00 00 00 f4 01 00 00 00 00 00 |NXSB............|
00000030
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
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000040
64
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#
セミコロンテスト(近い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#
ベストアンサー1
主な問題は、オプションがオペランドの前に指定されなければならないことです。便宜上、一部のGNUツールはそれを可能にするために与えられた引数を再配列します(ユーザーがそれについて考える必要がないように)、hexdump
GNU以外のシステムではこれを行うツールの1つではありません。
そのため、マニュアルの説明に従ってこれらのツールを使用してください。これはmacOSシステムからのものです(FreeBSDのマニュアルにも同じことがあります):
hexdump [-bcCdovx] [-e format_string] [-f format_file] [-n length]
[-s offset] file ...
つまり、コマンドラインを次のように並べ替えます。
hexdump -n "$stepSize" -Cv -s "$currPos" /dev/disk0s1
ファイル名の後にオプションを入れ、そのオプション-n
をそのままにして、すべてのhexdump
入力を読み続けると、最終的に-s
オプション引数を読み取るファイル名として使用しようとすることがわかります。
[...]
hexdump: -s: No such file or directory
hexdump: 12: No such file or directory
質問の後半は次のとおりです。
in値を使用すると、明示的なサブシェル内(つまり括弧内)で更新されるため、
$currPos
コマンド呼び出し間でin値は更新されません。(...)
親環境はサブシェル内で変更できず、サブシェル環境は終了時に削除されます。使用
{ ...; }
時には後にスペースが必要です{
。{
「複合コマンド」、つまり複数の異なるコマンドで構成されるコマンドを紹介するキーワードです。通常、シェル構文に単一のコマンド(パイプラインの単一ステップなど)が必要な場合、または単一のリダイレクトで複数のコマンドの出力をリダイレクトする場合は、これらの複合コマンドを使用します。
{ ...; }
if
ステートメントのand(or)の間に別々のコマンドがいくつかある可能性があるため、ステートメント内で使用する必要はありません。then
else
fi
if
if [ "$currPos" -le "$finalStep" ]; then hexdump -n "$stepSize" -Cv -s "$currPos" /dev/disk0s1 currPos=$((currPos + stepSize)) printf '%s\n' "$currPos" fi
break
これはループを中断するためにのみ使用されます。一行でこうなります
if [ "$currPos" -le "$finalStep" ]; then hexdump -n "$stepSize" -Cv -s "$currPos" /dev/disk0s1; currPos=$((currPos + stepSize)); printf '%s\n' "$currPos"; fi