常にbashコマンドを実行し、コマンドを(viモードを使用して)正しく編集し続けています。
これには、コマンドに戻ったり、コマンドの途中の場所にジャンプしたり、物事を変更したりすることがよくあります。私はこれをviモードでかなり速く行うことができますが、それでも遅くてエラーが発生しやすいことがわかります。
このようなコマンドをファイルに保存し、ファイルを編集して再実行することもありますが、これは私に考えさせられました...
たとえば、次のコマンドを繰り返しているとします。viモードを使用してコマンドに戻ったり、スクリプトで編集したりせずに、「test」、「super」、「java $」を更新する簡単な方法は何ですか?
$ find . | grep test | grep super | grep java$
別の例として、リモートサーバーに対してcurlを実行していて、別の引数も置き換えたい場合があります。次のように、変数を設定してから、それらをカールに渡すことがあります。
$ curl "http://example.com/$PATH/$PARMS"
しかし、これはより複雑になるため、変数を設定するコマンドなどを履歴で検索するのは多すぎると思います。
改善できると確信していますが、これをまとめて、サブシェルを使用するコマンドの途中でプロンプトを表示できるようにします。したがって、次の簡単な例では、実行するたびに2つのパラメーターをインタラクティブに要求し、その結果を出力します。
$ echo "hi $(ask name), how $(ask old) are you?"
:name (This is a prompt. I supplied "brad" as the value)
:old (This is another prompt. I supplied "young" as the value)
hi brad, how young are you?
もう一度実行すると、入力用に指定した内容が記憶されます(これらはの一時ファイルに保存されます/tmp
)。これは、2番目のパラメーターとして「no」を渡すことで無効にできます。
$ echo "hi $(ask name), how $(ask old) are you?"
:brad (This is a prompt. I supplied "parks" as the value)
:young (This is another prompt. I hit enter to use the default value)
hi parks, how young are you?
ask clear
ask help
read -i
古いbashでは利用できないため、これにはbash4.xが必要です。これは、アスクにデフォルト値を提供するためです。自作を使用してbashをアップグレードできます。これがスクリプトです!
#!/usr/local/bin/bash
me=$(basename "$0")
show_help()
{
it=$(cat <<EOF
Prompts you for info, allowing a default value that can also
be updated by being stored in a tmp file.
Useful for interactively replacing parts of a bash command
you're running with a new value easily, instead of having
to manually edit the command all the time.
usage: $me {default_value} {tmp_key}
e.g.
$me -> asks for input
$me HI -> asks for input, with "HI" supplied as default value.
Saves the user supplied value in a tmp file, and
uses that as the default the next time it's run
$me 1 no -> asks for input, with 1 supplied as default value.
If tmp_key is any value other than "no", will
save the user supplied value in a tmp file, and
use that as the default the next time it's run
$me clear -> removes any tmp files that have been created.
A more real example, the following will curl an url at
a site, and constantly ask you for the page you want to hit,
remembering the last value you used in a tmp file.
$ curl http://httpbin.org/\$($me somePage)
EOF
)
echo "$it"
exit
}
# Store tmp files in a folder
tmp_file_path="/tmp/__${me}"
mkdir -p "$tmp_file_path"
prompt=":"
if [ "$1" = "help" ]
then
show_help
fi
if [ "$1" = "clear" ]
then
rm -fr "$tmp_file_path"
echo "Removed any tmp files from $tmp_file_path"
exit;
fi
# Initialize our default value and tmp file name
default_value=${1:-""}
tmp_key_default=$(echo "$default_value" | head -n1 | tr -cd '[[:alnum:]]._-')
tmp_key=${2:-"no"}
if [ -n "$1" ]
then
if [ -z "$2" ]
then
tmp_key=$tmp_key_default
fi
fi
# Convert tmp_key to lower case
tmp_key=$(echo "$tmp_key" | tr '[:upper:]' '[:lower:]')
# Get the default value to prompt with
tmp_file="$tmp_file_path/$tmp_key"
if [ "$tmp_key" != "no" ]
then
if [ -f "$tmp_file" ]; then
default_value=$(cat "$tmp_file")
fi
fi
# Ask the user for input, supplying the default
read -r -e -p "$prompt" -i "$default_value" result
echo "$result"
# Save the new value to a tmp file if we're supposed to
if [ "$tmp_key" != "no" ]
then
echo "$result" > "$tmp_file"
fi
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加