VOOZH about

URL: https://qiita.com/tomiokario/items/986e8ee732730606622b

⇱ ShellScriptでよく使う表現 #Bash - Qiita


👁 Image
14

Go to list of users who liked

16

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

More than 5 years have passed since last update.

@tomiokario

ShellScriptでよく使う表現

14
Last updated at Posted at 2018-03-08

ShellScriptでよく使う表現を一般化したものをとりあえず置いておく場所

無限ループ

loop.sh
 while :
 do
	echo "コンパイルを開始します"
 echo "処理を実行"
	read -p "終了する場合はqを入力してください" res
	if [ $res = q ];then
	 break
	fi
 done

規則に従ったファイル名の決定

FileNameDecide_skip.sh
# !/bin/bash
statement=""
message="同名ファイルが存在します"
fileName ="newFile"
if ls | grep -w $fileName >/dev/null;then
 echo $message
else
 echo $statement > $fileName
fi

#####################################
<<readme
変数の説明
 massage : 同名ファイルが存在した時のメッセージ
 statement : 入力内容
 filename : 作成したいファイル名
readme
#####################################

指定時間毎に処理を行うスクリプト

repeater.sh
# !/bin/bash
secInterval=3
shFile="sample.sh"
message1="\n\n\n"
message2="\n\n\n"
while true;do
 printf $message1
 sh $shFile
 printf $message2
 sleep $secInterval
done

#############################
<<readme
変数の説明
 secInterval : 実行間隔(秒)
 shFile : 実行ファイルのpath
 message1 : 実行前に表示する文字(\nは改行)
 message2 : 実行後に表示する文字(\nは改行)
readme
############################

shellscript実行中に任意のコマンドを読み込むスクリプト

UserCommand.sh
# !/bin/bash
echo "終了時は'quit'と入力"
nowAd=`pwd`
read -p "${nowAd}$ " UserCom
until [ $UserCom = "quit" ];do
 $UserCom
 nowAd=`pwd`
 read -p "${nowAd}$ " UserCom
done

javaのdo while文のように一度処理を行う繰り返しを表現したスクリプト

DoWhile.sh
# !/bin/bash
read -p "数字を入力: " num
while $num==0 -o $num!=0 ;do
 read -p "数字を入力: " num
done

スクリプト中にディレクトリの移動を行わせるスクリプト

MoveDirectory.sh
# !/bin/bash
ad=`pwd`
cd; cd desktop
# 処理
cd; cd $ad
14

Go to list of users who liked

16
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14

Go to list of users who liked

16