壳牌:提示用户输入一个目录路径 [英] Shell: Prompt user to enter a directory path

查看:113
本文介绍了壳牌:提示用户输入一个目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个相当于猛砸Python的os.path.join的。我想提示输入目录路径,然后将要使用的用户(与路径的帮助下加入相当于)做其他的东西。

I'm looking for a Bash equivalent of Python's os.path.join. I'm trying to prompt the user for a directory path, which then will be used (with the help of path join equivalent) to do other stuff.

推荐答案

这是完全可以放心使用,例如,$ {dirpath} / $ {文件名}中一个庆典脚本。

It's perfectly safe to use, e.g., "${dirpath}/${filename}" in a bash script.

庆典既懂POSIX风格的路径名。即使你打造的MinGW /本地庆典在Windows上。这意味着 / 始终是路径分隔符。这也意味着,它绝不会伤害到把两个斜线连续,因此,即使 $ dirpath 发生在'/'结束,一切都很好。

bash only understands POSIX-style pathnames. Even if you build a MinGW/native bash on Windows. That means / is always the path separator. And it means that it never hurts to put two slashes in a row, so even if $dirpath happens to end in '/', everything is fine.

所以,例如:

$ cat join.sh
#!/bin/bash
echo -n 'Path: '
read dirpath
echo -n 'Filename: '
read filename
ls -l "${dirpath}/${filename}"
$ ./join.sh
Path: /etc/
Filename: hosts
-rw-r--r--  1 root  wheel  236 Sep 15  2014 /etc/hosts


在Python中,这不是安全的只使用 / 这种方式。 Python的处理原生格式路径名的POSIX和POSIX-like系统,也处理本机格式的路径名在Windows *,其中的路径分隔符是 \\ ,两个反斜杠有在某些地方有特殊的意义,你的驱动器盘符担心等,所以,你必须使用 os.path.join 可移植。


In Python, it's not safe to just use / this way. Python handles native-format pathnames on POSIX and POSIX-like systems, but also handles native-format pathnames on Windows.*, in which the path separator is \, two backslashes have a special meaning in certain places, you have drive letters to worry about, etc. So, you have to use os.path.join to be portable.

<子> *它也有code经典的Mac(使用冒号),VMS(它使用的是你不想知道不同的东西组合)等,如果你使用旧的Python足够

这篇关于壳牌:提示用户输入一个目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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