📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)
細 文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag… |
|||
| (同じ利用者による、間の3版が非表示) | |||
| 109行目: | 109行目: | ||
echo "${#array[@]}" | echo "${#array[@]}" | ||
# または | |||
echo "${#array[*]}" | |||
# 出力 | # 出力 | ||
4 | |||
4 | 4 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| 150行目: | 153行目: | ||
element = c | element = c | ||
element = d | element = d | ||
</syntaxhighlight> | |||
<br> | |||
配列の要素がスペースを含んでいる場合は、以下のように、配列をダブルクォーテーションで括る。<br> | |||
<syntaxhighlight lang="sh"> | |||
#!/bin/sh | |||
array=(1 2 3 4 5 "6 6") | |||
for i in "${array[@]}" | |||
do | |||
echo "${i}" | |||
done | |||
# 出力 | |||
1 | |||
2 | |||
3 | |||
4 | |||
5 | |||
6 6 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 174行目: | 197行目: | ||
hoge, , piyo, , fuga, | hoge, , piyo, , fuga, | ||
hoge, piyo, fuga, foo, bar, bazz | hoge, piyo, fuga, foo, bar, bazz | ||
</syntaxhighlight> | |||
<br><br> | |||
== 配列の要素をソートする == | |||
==== 文字列としてソートする ==== | |||
配列の全要素を出力した上で、<code>sort</code>コマンドでソートを実行して、実行結果を配列に格納する。<br> | |||
<br> | |||
まず、配列の要素に空白が含まれる場合を考慮して、IFSを改行のみに変更する。<br> | |||
これは、ソートした配列を生成する時、空白区切りで要素が設定されることを防ぐ。<br> | |||
次に、各要素を<code>echo</code>コマンドで出力することで、全要素が改行区切りで出力されるため、<code>sort</code>コマンドによるソートが可能になる。<br> | |||
<syntaxhighlight lang="sh"> | |||
#!/bin/sh | |||
IFS_BACKUP="$IFS" | |||
IFS="\n"; | |||
array=("222 222" "ccc ccc" "aaa aaa" "111 111" "bbb bbb" "333 333") | |||
array=$(for item in "${array[@]}"; do echo "$item"; done | sort) | |||
for element in "${array[@]}" | |||
do | |||
echo ${element} | |||
done | |||
IFS="$IFS_BACKUP" | |||
# 出力 | |||
111 111 | |||
222 222 | |||
333 333 | |||
aaa aaa | |||
bbb bbb | |||
ccc ccc | |||
</syntaxhighlight> | |||
<br> | |||
==== 数値としてソートする ==== | |||
要素を数値としてソートするには、<code>sort</code>コマンドに<code>-n</code>オプションを指定する。<br> | |||
<syntaxhighlight lang="sh"> | |||
#!/bin/sh | |||
IFS_BACKUP="$IFS" | |||
IFS="\n"; | |||
array=(2 3 04 000 001) | |||
array=$(for item in "${array[@]}"; do echo "$item"; done | sort -n) | |||
for element in "${array[@]}" | |||
do | |||
echo ${element} | |||
done | |||
IFS="$IFS_BACKUP" | |||
# 出力 | |||
000 | |||
001 | |||
2 | |||
3 | |||
04 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
| 347行目: | 431行目: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br><br> | <br><br> | ||
{{#seo: | |||
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki | |||
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 | |||
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux | |||
|image=/resources/assets/MochiuLogo_Single_Blue.png | |||
}} | |||
__FORCETOC__ | __FORCETOC__ | ||
[[カテゴリ:シェルスクリプト]] | [[カテゴリ:シェルスクリプト]] | ||