20220115のMacに関する記事は4件です。

シェルスクリプト 出力文字列の部分色付け Linux sh bash sed echo printf

概要 シェルスクリプト出力結果の一部を指定して色つけしたい 実行環境は Mac or RHEL系 ※ 今回は紫色にするので  色数字に035 → [35mを使用する 形式例 シンプルに色つけ ESC=$(printf '\033') echo "${ESC}[35m色をつけたい文字列${ESC}[m" 開始/終了記号も変数化してみた ESC=$(printf '\033');COLS=$(printf '[35m');COLE=$(printf '[m') echo "${ESC}${COLS}色をつけたい文字列${ESC}${COLE}/g" sed:複数指定する(-E使用時の)場合 # ; で区切って複数書く sed -E "s/条件1/変更内容1/g; s/条件2/変更内容2/g" sed:条件で拾った内容をそのまま出力に流用 # () で囲って \1 で出力 sed -E "s/(条件1)/\1/g" サンプル コマンド ESC=$(printf '\033');COLS=$(printf '[35m');COLE=$(printf '[m'); \echo -e "hogehttppiyo\nhttppiyo\nhogehttpshoge" | \sed -E "s/(^http.*$)/${ESC}${COLS}\1${ESC}${COLE}/g; s/(https)/${ESC}${COLS}\1${ESC}${COLE}/g" シェルスクリプト #!/bin/sh ESC=$(printf '\033') COLS=$(printf '[35m') COLE=$(printf '[m') TEXT1="hogehttppiyo" TEXT2=`echo "http"` TEXT="${TEXT1}\n${TEXT2}piyo\nhogehttpshoge" \echo -e $TEXT | \sed -E \ "s/(^http.*$)/${ESC}${COLS}\1${ESC}${COLE}/g;\ s/(https)/${ESC}${COLS}\1${ESC}${COLE}/g" # Macでは echo -e の -e は不要なので消す 出力結果 応用 echo内容を 変数 に置き換えれば色々流用できそう。 あとはsedの正規表現が組めるか次第 参考
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

LimaでDocker

LimaというVM環境でDockerを構築する。 22年1月18日シェル起動時にVM起動追記 https://github.com/lima-vm/lima 検証環境 Mac OS Monterey 12.1 MacBook Pro (Intel CPU) HomeBrew インストール済み 必要なパッケージをインストール brew install lima docker インストールが完了するとlimactlというコマンドが使用可能になる limactl --version 追加で入れておきたいパッケージをインストール brew install docker-compose docker.yamlを用意する 構築用のyamlを適当なディレクトリにdocker.yamlという名前で作成。 バージョンによって推奨する内容が微妙に変わるようなので、以下のURLへアクセスして内容をdocker.yamlという名前でファイルとして保存する。 github /lima-vm/lima/examples/docker.yaml https://github.com/lima-vm/lima/blob/master/examples/docker.yaml もしくは、wgetを使って以下のようにファイルをダウンロードする。 wget https://github.com/lima-vm/lima/raw/master/examples/docker.yaml docker.yamlに追記 リソースを変更 docker.yamlに以下を追記してリソースを変更。 cpus: 2 memory: "8GiB" disk: "100GiB" ssh関係 docker.yamlに以下を追記してエラー回避。 ~/.sshのディレクトリ以下のファイル関係で問題がある場合、loadDotSSHPubKeysをfalseにしておかないとlimactl startコマンドでINFO[0003] [hostagent] Waiting for the essential requirement 1 of 5: "ssh"のループに陥る。 ssh: loadDotSSHPubKeys: false また、ポートを指定する場合は、上記に追記してポート番号を指定する ssh: loadDotSSHPubKeys: false localPort: 60006 書き込み権限 デフォルトではユーザーのホームをマウントする設定になっているが、書き込みは出来ないようになっている。そのため、writable: trueで書き込み可能にするために、docker.yamlの一部書き換えが必要になる。 mounts: - location: "~" # 以下を追加 writable: true 実行 以下を実行して構築。 limactl start ./docker.yaml # --debugをつけると、エラーが発生した際に原因がわかりやすくなる # limactl start ./docker.yaml --debug 確認 構築出来ているか確認する limactl ls NAME STATUS SSH ARCH CPUS MEMORY DISK DIR docker Running 127.0.0.1:60006 x86_64 2 8GiB 100GiB /Users/ユーザ名/.lima/docker 仮想マシンに入る Dockerが構築されている仮想マシンに直接入るには、以下のコマンドを実行する。 入ったあとはexitで仮想マシンから出る。 limactl shell docker DOCKER_HOST 環境変数 .zshrcや.zprofile(MacOSのバージョンによっては.bash_profile)に DOCKER_HOST環境変数を追記して、Dockerのベースをlimaの仮想マシンで使用するように変更する。(Limaのバージョンによっては内容が少々変わる可能性がある) export DOCKER_HOST=unix:///${HOME}/.lima/docker/sock/docker.sock シェル起動時にVM起動 システムを再起動すると、作成した仮想環境はstopの状態になるので、先程の.zshrcに仮想環境を立ち上げるように追記する。 limactl start docker テスト nginxを構築 試しにnginxを構築 docker run --name nginx_test -d -p 8080:80 nginx http://localhost:8080/でnginxにアクセス出来るようになる。 nginxを削除 docker stop $(docker ps -aqf "name=nginx_test") docker rm $(docker ps -aqf "name=nginx_test")
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Rails+Reactで何か作る(パート1:Rails準備編)

はじめに rails と react で何か作ろうと思います。 やったことをメモ&共有します。 railsとreactは別プロジェクトとして作成します。 最終的に rails app は herokuへデプロイ react app は firebase hostingへデプロイ の予定です。 今回は、RailsAppを作成してテスト用のAPIを作成するところまでを行います。 環境 ツール バージョン ruby 3.0.2p107 sqlite3 3.36.0 node v17.3.0 yarn 1.22.17 rails 7.0.0 git 2.26.2 Railsの準備 Railsをnewする APIモードでrails アプリを作成する $ rails new quiz_rails --api とりあえず、git で first commit しておく $ cd quiz_rails $ git status On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .gitattributes .gitignore .ruby-version Gemfile Gemfile.lock README.md Rakefile app/ bin/ config.ru config/ db/ lib/ log/ public/ storage/ test/ tmp/ vendor/ $ git add . $ git commit -m "rails new quiz_rails --api" とりえあず、起動してみる $ rails server => Booting Puma => Rails 7.0.1 application starting in development => Run `bin/rails server --help` for more startup options Puma starting in single mode... * Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi") * Min threads: 5 * Max threads: 5 * Environment: development * PID: 9149 * Listening on http://127.0.0.1:3000 * Listening on http://[::1]:3000 Use Ctrl-C to stop http://localhost:3000 をブラウザで表示 テストAPIを作成 モデルを作成 $ rails g model Chat content:string invoke active_record create db/migrate/20220115021134_create_chats.rb create app/models/chat.rb invoke test_unit create test/models/chat_test.rb create test/fixtures/chats.yml db/migrate/20220115021134_create_chats.rb class CreateChats < ActiveRecord::Migration[7.0] def change create_table :chats do |t| t.string :content t.timestamps end end end マイグレートしておく $ rails db:migrate == 20220115021134 CreateChats: migrating ====================================== -- create_table(:chats) -> 0.0028s == 20220115021134 CreateChats: migrated (0.0029s) ============================= テスト用のデータを作る db/seeds.rb Chat.create(content: "Hello, world!") Chat.create(content: "これはSeedで作ったメッセージです") $ rails db:seed コントローラーを作る $ rails g controller api/v1/chats create app/controllers/api/v1/chats_controller.rb invoke test_unit create test/controllers/api/v1/chats_controller_test.rb app/controllers/api/v1/chats_controller.rb class Api::V1::ChatsController < ApplicationController before_action :set_chat, only: %i[show destroy update] def index chats = Chat.all.order(:id) render json: chats end def show render json: @chat end def create chat = Chat.new(chats_params) if chat.save render json: chat else render json: chat.errors end end def update if @chat.update(chats_params) render json: @chat else render json: @chat.errors end end def destroy if @chat.destroy render json: @chat else render json: @chat.errors end end private def set_chat @chat = Chat.find(params[:id]) end def chats_params params.require(:chat).permit(:content) end end curl コマンドで確認 $ curl http://localhost:3000/api/v1/chats [{"id":1,"content":"Hello, world!","created_at":"2022-01-15T02:27:46.274Z","updated_at":"2022-01-15T02:27:46.274Z"},{"id":2,"content":"これはSeedで作ったメッセージです","created_at":"2022-01-15T02:27:46.279Z","updated_at":"2022-01-15T02:27:46.279Z"}] git にコミット $ git status On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: config/routes.rb modified: db/seeds.rb Untracked files: (use "git add <file>..." to include in what will be committed) app/controllers/api/ app/models/chat.rb db/migrate/ db/schema.rb test/controllers/api/ test/fixtures/chats.yml test/models/chat_test.rb no changes added to commit (use "git add" and/or "git commit -a") $ git add . $ git commit -m "add test api" おわりに。 久々にrails触りましたが、基本的なことは昔(3年前)くらいと変わってなさそうですね ControllerとModelを別々に rails g しましたが、 そういえば、一発で作るとか出来たような、そっちでやればよかったかなとも思っています。 今度やってみます。 おわり。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

pyenvによるPythonのバージョン変更方法 for Mac

参考 概要 Pythonのバージョン切り替え方法について 内容 MacでPythonのバージョンを切り替える場合はpyenvを使用する インストール pyenvのインストール % brew update % brew install pyenv シェルの環境設定 (zshを使用している場合) % echo 'eval "$(pyenv init --path)"' >> ~/.zprofile % echo 'eval "$(pyenv init -)"' >> ~/.zshrc 使用方法 インストール可能なバージョンの確認 % pyenv install --list 指定したバージョンのインストール (今回はバージョン3.7.12をインストール) % pyenv install 3.7.12 インストールしたバージョンの確認 % pyenv versions * system (set by /Users/username/.pyenv/version) 3.7.12 systemは元々入っているPython 3.7.12が今回インストールしたバージョン *(アスタリスク)がついているバージョンが現在適用されている バージョン切り替え % pyenv shell 3.7.12 バージョンの確認 % pyenv versions system * 3.7.12 (set by PYENV_VERSION environment variable) もとに戻すときは % pyenv shell --unset * system (set by /Users/username/.pyenv/version) 3.7.12 補足 Macにはデフォルトでpython2が入っておりコマンド"python ~"ではpython2.xが実行され python 3.xを実行するには"python3 ~"で実行する必要がある。 しかし、pyenvで変更されるのはコマンド"python"の方なのでバージョンを変更したpythonを実行する際は"python ~"で実行する必要がある。 (コマンド"python3"は変更されない) バージョン変更前 % python -V Python 2.7.16 % python3 -V Python 3.8.5 バージョン変更後 % pyenv shell 3.7.12 % python -V Python 3.7.12 % python3 -V Python 3.8.5 以上
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む