grepコマンドで終了ステータスコード(grep -p ~)を取得

grepコマンドで特定の文字を含む場合に、下記のようにgrepコマンドとwcコマンドを利用して検索結果の行数を取得してif文で分岐をよく使っています
#!/bin/bashcnt=$(grep ERROR test.log | ...Cronで、MySQLのSQLクエリを定期的に実行する

MySQLのDBのメンテンナンスで定期的にupdate, delete分を実行したい場合があります。
そのようなケースではMySQLのSQL文を実行するシェルスクリプト ...
csvファイルの指定したカラムの空白文字を変換する

csvファイルで指定したカラム内の値がファイル名でWindowsだと良いのですが、Linuxだと良くないので半角の空白をアンスコ(_)やハイフン(-)に変換したくて、シェルスクリプトで頑張って色々とやってみましたがうまくいきませんでし ...
「grep -E」で正規表現を使って検索する【grep】

grepは指定したファイルなどから、特定の文字列を検索できるコマンドです。
grepで正規表現を使いたい場合は「egrep」を使えば良いのですが、「-E」のオプションを使うとgrepコマンドで、拡張正規表現 (Extended ...
「新しいBing」でプロセスを監視するシェルスクリプトを作成

前回はChatGPTでプロセス監視の記事(ChatGPTでプロセスを監視するシェルスクリプトを作成)を書きましたが、マイクロソフトの「新しいBing」も使用できるようになったので、こちらでもプロセス監視のシェルスクリプトを作って貰いま ...
ChatGPTでプロセスを監視するシェルスクリプトを作成

最近、話題になっている「ChatGPT」を利用して簡単なシェルスクリプトを作成してみました。
「ChatGPT」は、OpenAIが開発した対話型AIチャットボットです。対話型で質問をすればAIが回答してくれます。
...
シェルスクリプトで「integer expression expected」のエラー

昔作ったシェルスクリプトで久しぶりに実行してみたら、下記のように「integer expression expected」とエラーが上がって処理ができていませんでした
sh test.sh: line 6: ; then ec ...除外リストにある文字を含む行を削除する方法「シェルスクリプト」

アカウント・ディレクトリ等の一覧のをファイルが有り、そのファイル内で特定の文字を含む行を削除したい場合は、「sed」コマンドを利用すればできます。
その削除したい文字列を除外リストのようなファイルにある場合は、while re ...
sedコマンドでHTMLタグの中身を取得する

HTMLソースで以下のようなタグの場合、<h2>~</h2>に囲まれた「TEST」のみを取得したい場合ですが、sedコマンドを利用するとタグで囲まれた中身を取得できます
<h2>test< ...行頭にゼロに入った数値の文字列で0を解除する【シェルスクリプト】

0で埋められた「001」、「0010」等の文字列になっている数字で、先頭からある「0」を外したい場合があります。
sedコマンド「s/0*\(*$\)/ ...
シェルスクリプトの if文で複数の条件を指定する

シェルスクリプトでif文でAND条件やOR条件を複数指定したい場合ですが、以下のようにすればOKです
if文でAND条件の場合AND条件では「条件1 -a 条件2」と条件を「-a」を付与して続け ...
date コマンドで前月を取得する

dateコマンドで前月の日付を取得するには「1 month ago」のオプションを付けると一ヶ月前の値を取得ることが出来ます。
$ date +'%Y-%m-%d'2020-11-04$ date -d '1 month ago ...SSL証明書の有効期限をメールで通知するスクリプト

無料のSSL証明書の「Let’s Encrypt」を利用しています。有償のSSL証明書と比べると有効期限が短いので更新を忘れそうなので、SSL証明書の有効期限をチェックしてその期限をメールするスクリプトを作成しました
ce ...
lsコマンドで、UNIXTIME(エポック秒)を取得する

ファイルのタイムスタンプを比較してから〇〇分経過していない場合は、Aの処理するようなシェルスクリプトを作る必要になり、シェルスクリプトで時間計算する場合はUnixTimeに変換する必要があるので、lsコマンドでタイムスタンプをUnix ...
シェルスクリプトの変数名を数字で初めていけない

他のプロブラムでもそうですが、変数名に数字で始めるとシェルスクリプトではエラーになります
#!/bin/bash1str="TEST"echo "$1str"$sh test.shtest.sh: 行 3: 1str=: コマン ...