一个命令创建和更改目录 [英] one command to create and change directory

查看:120
本文介绍了一个命令创建和更改目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索命令—没有&& | —创建一个目录,然后立即将当前目录更改为新创建的目录。 (这是一个有人为他的linux使用的考试的问题,他做了一个新的命令,这样做,但没有给他的点。)这是在debian服务器,如果这是重要的

$ HOME / .bashrc 中为此目的定义

解决方案

code>例如

  function mkdcd(){
mkdir$ 1&& cd$ 1
}

然后输入 mkdcd foodir 在你的交互式shell中



如此严格的意义,如果没有一个 shell函数一些&&& (或至少一个; )...换句话说,是为了让你理解为什么函数(或别名)在shell中非常有用....

PS它应该是一个函数,而不是一个脚本如果它是一个脚本, cd 将仅影响运行脚本的[子] shell,而不影响交互式父shell。不可能做出单个命令或可执行文件(不是shell函数),这将改变调用交互式父shell的目录(因为每个进程都有自己的当前目录,并且只能更改自己进程的当前目录,而不是调用shell进程)。



PPS。在Posix shell中,你应该删除 function 关键字,并且第一行为 mkdcd(){


I'm searching for just one command — nothing with && or | — that creates a directory and then immediately changes your current directory to the newly-created directory. (This is a question someone got for his exams of "linux-usage", he made a new command that did that, but that didn't give him the points.) This is on a debian server if that matters

解决方案

define a bash function for that purpose in your $HOME/.bashrc e.g.

 function mkdcd () {
     mkdir "$1" && cd "$1"
 }

then type mkdcd foodir in your interactive shell

So stricto sensu, what you want to achieve is impossible without a shell function containing some && (or at least a ; ) ... In other words, the purpose of the exercise was to make you understand why functions (or aliases) are useful in a shell....

PS it should be a function, not a script (if it was a script, the cd would affect only the [sub-] shell running the script, not the interactive parent shell); it is impossible to make a single command or executable (not a shell function) which would change the directory of the invoking interactive parent shell (because each process has its own current directory, and you can only change the current directory of your own process, not of the invoking shell process).

PPS. In Posix shells you should remove the functionkeyword, and have the first line be mkdcd() {

这篇关于一个命令创建和更改目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆