题
我有一个简单的功能打开一个编辑:
open_an_editor() { nano "$1" }
如果喜欢 open_an_editor file.ext
, 它的工作。但是,如果我需要得到一些输出功能 smth=$(open_an_editor file.ext)
—我看不见编辑、脚本就被堵塞.我是什么丢在这里?
更新:我想写一个功能,这将要求用户编写的价值在编辑,如果不是给予在脚本参数。
#!/bin/bash open_an_editor() { if [ "$1" ] then echo "$1" return 0 fi tmpf=$(mktemp -t pref) echo "default value, please edit" > "$tmpf" # and here the editor should show up, # allowing user to edit the value and save it # this will stuck without showing the editor: #nano "$tmpf" # but this, with the help of Kimvais, works perfectly: nano "$tmpf" 3>&1 1>&2 2>&3 cat "$tmpf" rm "$tmpf" } something=$(open_an_editor "$1") # and then I can do something useful with that value, # for example count chars in it echo -n "$something" | wc -c
因此,如果脚本是用参数 ./script.sh "A value"
, ,职能将只使用,并立即回声7字节。但是,如果称为没有参数 ./script.sh
—纳米应该弹出来。
其他提示
如果你需要从在多元函数和存储输出,你只显示有什么文件。
open_an_editor()
{
cat "$1"
}
smth=$(open_an_editor file.txt)
如果你想要的是一个用户输入一个值,则read
是不够的:
OLDIFS="$IFS"
IFS=$'\n'
read -p "Enter a value: " -e somevar
IFS="$OLDIFS"
echo "$somevar"
不隶属于 StackOverflow