![](/img/trans.png)
[英]How to pass a variable along with file name in single line sftp put command
[英]Create file along with nested directory in single command line
如果我理解正確,並且您只是想發出command foo/bar/baz/myfile.txt
(或類似的命令),並創建目錄foo/bar/baz
和一個新文件myfile.txt
,並在nano
打開通過一個命令全部完成,那么您只需要一個簡短的腳本,例如
使它可執行,例如mv nanoopen.sh scriptname; chmod 0755 scriptname
mv nanoopen.sh scriptname; chmod 0755 scriptname
,然后只需調用./scriptname
foo/bar/baz/file.txt
。 如果將其放在路徑中, 也可以跳過./ 。
放置在路徑中的簡單方法是在/usr/local/bin
創建指向它的符號鏈接,該鏈接通常在默認路徑中。 因此,您可以(有時需要確保) ln -s /path/to/nanoopen.sh /usr/local/bin/scriptname
。 回顯$ PATH以確認/ usr / local / bin在您的路徑中,然后將其像任何程序,腳本名參數一樣使用。 或者在某些發行版中,您可以簡單地將其添加到具有root訪問權限的/bin
文件夾中。
#!/bin/bash
[ -z "$1" ] && { ## validate one argument given
printf "error: insufficient input\nusage: %s filename\n" "${0##*/}"
exit 1
}
[ "$1" != "${1##*/}" ] && mkdir -p "${1%/*}" ## if it has directories, create
touch "$1" ## create the file
exec nano "$1" ## open in nano
使用/輸出示例
$ bash nanoopen.sh foo/bar/baz/main.c
$ tree foo/
foo/
└── bar
└── baz
└── main.c
$ cat foo/bar/baz/main.c
My new source!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.