echo "__start bash__"
echo ""
echo "____スクリプト"
echo "script_name:$0"
echo "script_args_count:$#"
echo "____文字列"
name="john_abcdefg"
echo $name
echo "xyz_${name}"
echo "部分文字列:${name:0:3}"
echo "部分文字列:${name:4}"
echo "length:${#name}"
echo "upper:${name^^}"
echo "lower:${name,,}"
foo=""
echo "default:${foo:=default-val}"
echo "____コマンド実行"
pwd
echo "dir:$(pwd)"
echo "____関数"
function get_name() {
echo "$1 - $2"
}
myname=$(get_name "john" "punch")
echo "myname:$myname"
function get_value() {
return 100
}
get_value
echo "ret:$?"
echo "____パス"
path="/abc/def/ghi.cs"
echo "${path##*/}"
echo "${path%/*}"
echo "____色付け"
tput setaf 1 && "hello world red"
tput setaf 2 && "hello world green"
tput setaf 3 && "hello world yellow"
tput setaf 7 && "hello world white"
echo "____制御"
echo "__for"
for v in {1..3}; do
echo "$v"
done
echo "__if"
state_str="abc"
if [[ $state_str == "abc" ]]; then
echo "if-match-str"
fi
state_val=110
if [[ $state_val -gt 100 ]]; then
echo "if-match-value"
fi
state_reg="123abc"
if [[ $state_reg = ^[0-9]. ]]; then
echo "if-match-regex"
fi
echo "__ファイル条件"
if [[ -e file.txt ]]; then
echo "exists > file.txt."
fi
echo "__case"
cv="v2"
case "$cv" in
"v1")
echo case_v1
;;
"v2" | "v3")
echo case v2 or v3
;;
*)
echo default
;;
esac
echo "____配列"
fruits=('apple' 'banan' 'orange')
echo "fruits-2:${fruits[2]}"
echo "__宣言"
declare -a item_array=()
item_array+=("abc")
item_array+=("def")
echo "item:${item_array[0]}"
echo "item:${item_array[1]}"
for item in "${item_array[@]}"; do
echo "for-item:$item"
done
echo "____辞書"
declare -A dic
dic[a]=100
dic[b]=200
dic[c]=300
echo "dic-a:${dic[a]}"
echo "dic-b:${dic[b]}"
echo "dic-b:${dic[c]}"
echo "__辞書_key"
for key in "${!dic[@]}"; do
echo "key:$key"
done
echo "__辞書_value"
for value in "${dic[@]}"; do
echo "value:$value"
done
echo "____ファイル読込"
while read -r line; do
echo "$line"
done <file.txt
echo "____コマンドの結果"
echo "__ファイル一覧"
for file in *.txt; do
if [ -f "$file" ]; then
echo "item:$file"
fi
done
echo "__ファイル一覧2(IFSの半角スペース・タブ・改行を無視し、オリジナルの内容を取得する)"
find ./*.txt | while IFS= read -r line; do
echo "item:$line"
done
echo "__コマンド結果一覧"
ps aux | head -3 | while read -r line; do
echo "item:$line"
done
echo "__コマンド結果一覧(awk, sort)"
ps aux | awk '{ print $3, $4, $5, $11 }' | sort -k 3 -nr | head -3
echo "__コマンド結果一覧(配列に追加、参照、ファイルに出力)"
declare -a ps_array=()
while IFS= read -r line; do
ps_array+=("$line")
done < <(ps aux | grep /lib/systemd)
outpufile="output_psaux.txt"
rm $outpufile
for item in "${ps_array[@]}"; do
echo "item:$item"
echo "$item" >>$outpufile
done
echo "__コマンドの出力結果を文字列で出力"
echo "dir:$(pwd)"
echo "____他コマンドとの連携"
echo "__awk(抽出)"
ls -al | awk '{ print $9, $5 | "sort -k2,2 -n -r" }'
echo "__cut(抽出)"
echo "a,b,c,d,e,f,g" | cut -d ',' -f 3-4
echo "__grep(検索)"
ps -aux | grep systemd
echo "__sed(置換、削除、抽出など)"
ps aux | sed s/systemd/SYSTEMD/g
echo "____設定ファイルから読込み"
source ./setting.config
tput setaf 2 && echo "MY_VALUE1:$MY_VALUE1"
tput setaf 2 && echo "MY_VALUE2:$MY_VALUE2"
tput setaf 7
echo ""
echo "__end bash__"