Last active
June 12, 2024 03:26
-
-
Save sdkfz181tiger/78cb889f46322cbb752396f69a01c5a2 to your computer and use it in GitHub Desktop.
シェルスクリプトあっさり7選!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# シェルスクリプトの実行と引数 | |
# $bash test.sh | |
# $bash test.sh hoge | |
# $bash test.sh hoge fuga | |
# $bash test.sh hoge fuga piyo | |
echo "Running $0" | |
echo "arg1: $1" | |
echo "arg2: $2" | |
echo "arg3: $3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 制御構文_if | |
# ファイルが存在するか判定 | |
# $bash test.sh | |
echo "Running $0" | |
txt="./story.txt" | |
if [ -f $txt ] ; then | |
echo "File is exists." | |
#cat $txt | grep -oP "吾輩は.*?。" | |
else | |
echo "File is not exists..." | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 制御構文_case | |
# grepの結果を配列に格納し確認する | |
# $bash test.sh 2 | |
# -eq -> equal | |
# -ne -> not equal | |
# -lt -> less than | |
# -le -> less equal | |
# -gt -> greater than | |
# -ge -> greater equal | |
echo "Running $0" | |
if [ $# -gt 0 ] ; then | |
case $1 in | |
1) echo "吾輩は" ;; | |
2) echo "猫である。" ;; | |
3) echo "名前はまだ無い" ;; | |
4) echo "ですけど何か問題でも!?" ;; | |
esac | |
else | |
echo "No parameters..." | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 制御構文_for | |
# grepの結果を配列に格納し確認する | |
# $bash test.sh | |
echo "Running $0" | |
txt="./story.txt" | |
if [ -f $txt ] ; then | |
echo "File is exists." | |
msgs=($(grep -oP "吾輩は.*?。" $txt)) | |
# for | |
echo "= This is for =" | |
for msg in ${msgs[@]} | |
do | |
echo $msg | |
done | |
else | |
echo "File is not exists..." | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 制御構文_while | |
# grepの結果を配列に格納し確認する | |
# $bash test.sh | |
echo "Running $0" | |
txt="./story.txt" | |
if [ -f $txt ] ; then | |
echo "File is exists." | |
msgs=($(grep -oP "吾輩は.*?。" $txt)) | |
# while | |
echo "= This is while =" | |
len=${#msgs[*]} | |
i=0 | |
while [ $i -lt $len ] | |
do | |
echo $i ${msgs[$i]} | |
i=$((i+1)) | |
done | |
else | |
echo "File is not exists..." | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 制御構文_function | |
# 関数の利用 | |
# $bash test.sh | |
echo "Running $0" | |
hoge(){ | |
echo "Hello, Nobunaga!!" | |
} | |
fuga(){ | |
echo "Hello, Hideyoshi!!" | |
} | |
greeting(){ | |
echo "Good morning" $1 "," $2 "how are you today!?" | |
} | |
hoge | |
fuga | |
greeting Ieyasu Date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# seq | |
# 連続した数を生成する | |
# $bash test.sh | |
echo "Running $0" | |
for i in `seq 0 100` | |
do | |
echo "counter" $i | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment