0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

NO MORE “cd: foo: No such file or directory”

Posted at

二度と “cd: foo: No such file or directory” と言わせない

一度はやるよね.

存在しないディレクトリに入ろうとする
$ cd foo
bash: cd: foo: No such file or directory

そして fuck と叫びながら fuck する.

fuck する
$ fuck
mkdir -p foo && cd foo [enter/↑/↓/ctrl+c]

ここで与えられた fuck を見て “これだー” ってなった.

“これだー” is どれだー

こんな感じの処理をすればいいのでは?
(入ろうとするディレクトリの名前は foo)

フローチャート

もうあとはこれを書けばいいだけ.

試し書き
function cd() {
    if [ -d $1 ]; then
        command cd $1
    elif [ ! -e $1 ]; then
        mkdir -p $1 && command cd $1
    else
        echo もうだめ
    fi
}

でもエラー表示が もうだめ なのはいかがなのだろうか.

もう少し考える

ファイルに対して cd しようとするともちろん怒られる.

$ touch hoge
$ cd hoge
bash: cd: hoge: Not a directory

これでいいんじゃないか.

フローチャート

とりあえず本命
function cd() {
    if [ ! -e $1 ]; then
        mkdir -p $1 && command cd $1
    else
        command cd $1
    fi
}

おわりに

もっと賢い方法があれば教えてください.

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?