
変数からいくつかの名前を除外したいです。
# echo $names
abba begiz altonzon music aolala
# echo $names | grep -o '[^[:space:]]\+'
abba
begiz
altonzon
music
aolala
egrep
以下を使用してこれら2つの名前を除外すると、
その後、次の例外が発生します。"shell-init: error retrieving current directory:"
# echo $names | grep -o '[^[:space:]]\+' | egrep -iv "abba|begiz"
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
altonzon
music
aolala
この例外を回避する方法は?
答え1
egrep
これは、一部のシステム、少なくともDebianのシェルでは、削除されたディレクトリから起動するのが好きではないシェルスクリプトです。
$ mkdir /tmp/z
$ cd /tmp/z
$ rm -r /tmp/z
$ egrep
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
これがBash as /bin/sh
Dashが提供するものです。sh: 0: getcwd() failed: No such file or directory
grep -E
シェルスクリプトの実行をバイパスしたり、削除されたディレクトリで実行したくない場合は、直接使用してください。
まったく異なる点は、現在実行中のタスクを実行するために次の問題が発生しないより良い方法がある可能性があることです。噴射そしてワイルドカード。
Bashでは配列を使用できます。
names=(abba begiz altonzon music aolala)
newnames=()
for x in "${names[@]}"; do
if [[ ! $x =~ ^(abba|begiz)$ ]]; then
newnames+=("$x")
fi
done
# do something with newnames