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

【Mac】aliasの設定方法(再起動しても消えない)

概要

Macのターミナルにて、aliasの設定方法のメモ

環境

・端末:MacOS Catalina ver.10.15.5
・ターミナル:iTerm2

方法

  1. ターミナルを開き、~/.zshrcファイルを編集する。
$ vim ~/.zshrc
  1. 設定したいaliasのコマンドを設定する
~/.zshrc
alias ll='ls -l'
alias lla='ls -la'
alias em='emacs'
  1. ターミナルを再起動するか以下のコマンドを実行
$ source ~/.zshrc

これで、電源を入れ直してもaliasは消えず設定される

参考

zshにaliasで設定したものが反映されない

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

【小ネタ】Ansibleでpingを飛ばせない場合の対処方法

はじめに

Mac上にbrewでAnsibleをインストールしました。(Ansible勉強中)
そこで、pingの実行に少々苦戦をしましたので、対処方法を載せたいと思います。

事象

1つの対象にpingを実行すると、下記のようなエラーが表示される。

実行結果
% ansible -i 192.168.56.51 all -m ping --connection=local 
[WARNING]: Unable to parse /Users/satton/etc/ansible/192.168.56.51 as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
% 

対処方法

IPアドレスの後に,を付ける。

実行結果
%  ansible -i 192.168.56.51, all -m ping --connection=local
[WARNING]: Platform darwin on host 192.168.56.51 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
192.168.56.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
% 


※複数ホストにpingを実行することも可能

実行結果
% ansible -i 192.168.56.51,8.8.8.8,8.8.4.4 all -m ping --connection=local
[WARNING]: Platform darwin on host 8.8.4.4 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
8.8.4.4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform darwin on host 192.168.56.51 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
192.168.56.51 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[WARNING]: Platform darwin on host 8.8.8.8 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
8.8.8.8 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
% 

参考

[小ネタ] Pythonのバージョンを指定してAnsible実行時に表示される警告を消す

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Mac Javaホームディレクトリ

Macで、Javaのバージョンアップをする際に、毎回ホームディレクトリの場所を忘れてしまう。

.bashrc に記載している環境変数を見て、「そーだった」と納得しつつ、覚えられない自分がいる。

export JAVA_HOME="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home"

ついでに brewでのJavaインストールコマンドも残しておきます。

brew tap AdoptOpenJDK/openjdk
brew cask install adoptopenjdk14

https://github.com/AdoptOpenJDK/homebrew-openjdk

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

[macOS]シェルスクリプトで簡易的なカウントダウンタイマーを作成する

概要

本書はシェルスクリプトを使用して簡易的なカウントダウンタイマーを作成します。

前提条件

  • Macから音が出るようにボリュームを調整してください。
  • 本書ではMacBookAir2019(macOS Catalina)を使用しました。

スクリプト内容

#!/bin/sh

<< COMMENTOUT
countdown_timer(macOS)
Copyright (c) 2020 yuichi1992_west
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
COMMENTOUT

# A variable that checks whether only a numerical value (seconds) is entered using a regular expression.
timer_count='[0-9]+'

# Prompt for the time to play the sound (specify the number of seconds).
echo "Prompt for the time to play the sound (specify the number of seconds)."
echo "For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day."
echo "After entering, press Enter.  "
read timer_seconds

# Compare the entered value with the regular expression.
if [[ $timer_seconds =~ $timer_count ]] ;
then

    # If the regular expression is correct, start the countdown.
    echo ""
    echo "Start the countdown."

    # Increments by 1 with a for statement from 0 to the entered value.
    for ((i=0;i<$timer_seconds;i++))
    do

        # Output the remaining time.
        echo ""
        echo "Remaining time (seconds)"
        expr $timer_seconds - $i
        echo ""

        # Wait 1 second.
        sleep 1

    done

    # Sound the alarm (standard audio of macOS) 10 times.
    echo "It's time! Play the alarm!"
    for ((k=0;k<=10;k++))
    do
        afplay /System/Library/Sounds/Ping.aiff
    done

    read -p "Press [Enter] key to resume."

else

    # If the entered value is not a number, a warning will be prompted.
    echo ""
    echo "Important!! Characters other than numbers are included. Enter only half-width numbers from 0 to 9."
    echo "For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day."
    echo ""

    read -p "Press [Enter] key to resume."

fi

exit 0      

動作結果

成功の場合

% bash countdown_timer.sh
Prompt for the time to play the sound (specify the number of seconds).
For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day.
After entering, press Enter.
5

Start the countdown.

Remaining time (seconds)
5


Remaining time (seconds)
4


Remaining time (seconds)
3


Remaining time (seconds)
2


Remaining time (seconds)
1

It's time! Play the alarm!

(システムで用意されている音声を10回鳴らします)

Press [Enter] key to resume.
  • 途中でアラームを止める場合はcontrol + Cを押してください。

失敗の場合

% bash countdown_timer.sh
Prompt for the time to play the sound (specify the number of seconds).
For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day.
After entering, press Enter.
aaa

Important!! Characters other than numbers are included. Enter only half-width numbers from 0 to 9.
For example, enter [1] for 1 second, [60] for 1 minute, [3600] for 1 hour, and [86400] for 1 day.

Press [Enter] key to resume.

動作について

  • 正規表現(数値のみ)の変数を宣言する。
timer_count='[0-9]+'
  • 音声を鳴らす時間をユーザーに入力させる。
read timer_seconds
  • 入力した値が全て数値(0-9)であるか判定する。
if [[ $timer_seconds =~ $timer_count ]] ;
  • for文を利用してカウントダウンを始める。1秒ごとにiの値を1増やし、入力した時間になったらfor文から抜ける。
for ((i=0;i<$timer_seconds;i++))
  • 残り時間(入力した時間 - iの値)を計算する。
expr $timer_seconds - $i
  • 1秒待機する。
sleep 1
  • iの値が入力した時間に達したら音声を10回鳴らす。
for ((k=0;k<=10;k++))
  • システムで用意された音声を鳴らす。
afplay /System/Library/Sounds/Ping.aiff
  • 10回鳴らした後、ユーザーにEnterキーを押させてスクリプトを終了する。
read -p "Press [Enter] key to resume."

最後に

ターミナルを立ち上げて本書のシェルスクリプトを実行することでカウントダウンタイマーが利用できます。またfor ((k=0;k<=10;k++))からkの値を変更してアラーム回数を増減させたり、afplay /System/Library/Sounds/Ping.aiffから別のシステム音声や好きな曲に変更する、というカスタマイズもできます。

  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む