[英]Bash script to open a terminal and cd to a variable directory
I want to write a bash script that opens a gnome-terminal in a directory in ~/Documents/
according to the argument passed to it (eg ./open.sh notes
opens the terminal in ~/Documents/notes/
) 我想编写一个bash脚本,根据传递给它的参数在
~/Documents/
目录中打开一个gnome-terminal(例如./open.sh notes
在~/Documents/notes/
打开该终端)
How would I go about that? 我将如何处理? I know
gnome-terminal --working-directory=[directory]
does something similar, but it doesn't accept strings so I don't know if it can be used in this case. 我知道
gnome-terminal --working-directory=[directory]
做类似的事情,但是它不接受字符串,所以我不知道在这种情况下是否可以使用它。
you can try this; 你可以试试看
#!/bin/bash
workDir="/home/user/Documents/$1"
if [ ! -d "$workDir" ]; then
echo "directory not found, check your path"
else
gnome-terminal --working-directory="$workDir"
fi
Example; 例;
./open.sh notes
this open a new terminal in ~/Documents/notes 这会在〜/ Documents / notes中打开一个新终端
Your script, or function, could simply be: 您的脚本或函数可以简单地是:
open() {
gnome-terminal --working-directory="Documents/$1"
}
It looks like the working directory can be specified relative to the user's home directory. 看起来可以相对于用户的主目录指定工作目录。
Use it like open notes
to open a new terminal in ~/Documents/notes
. 像
open notes
一样使用它在~/Documents/notes
打开一个新终端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.