rɑː*_*dʒɑ 20 command-line bash
我需要在行或 URL 中替换一个单词,但我需要从命令行/终端进行。
我的意思是
$ ./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code1 -t query
Run Code Online (Sandbox Code Playgroud)
现在从那里开始,无需返回,我需要替换code1为mycode或其他一些字符串。
Jef*_*ler 50
我重命名了您的脚本,但这里有一个选项:
$ ./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code1 -t query
Run Code Online (Sandbox Code Playgroud)
执行脚本后,使用:
$ ^code1^code2
Run Code Online (Sandbox Code Playgroud)
...这导致:
./myscript.sh xxx.xxx.xxx.xxx:8080/code -c code2 -t query
Run Code Online (Sandbox Code Playgroud)
man bash 并搜索“事件指示符”:
^字符串1^字符串2^
快速替代。重复上一条命令,将 string1 替换为 string2。相当于 !!:s/string1/string2/
编辑添加全局替换,我刚刚从@slm 在https://unix.stackexchange.com/a/116626/117549的回答中了解到:
$ !!:gs/string1/string2
Run Code Online (Sandbox Code Playgroud)
其中说:
!! - recall the last command
g - perform the substitution over the whole line
s/string1/string2 - replace string1 with string2
Run Code Online (Sandbox Code Playgroud)
bash 内置命令fc可用于在历史记录中查找命令并可选择编辑/运行它。使用 bash 内置命令help fc获取更多文档。
fc -s code1=code2
Run Code Online (Sandbox Code Playgroud)
会查找上一条命令中所有出现的code1并将其更改为code2,然后执行新命令。
当需要更改多个特殊字符时,它会很有用。假设之前的命令是;
$ java a/b/c/d
# Then,
fc -s /=.
# will produce
$ java a.b.c.d
Run Code Online (Sandbox Code Playgroud)