Shell脚本波浪号扩展 [英] Shell Script Tilde Expansion

查看:187
本文介绍了Shell脚本波浪号扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的剧本:

#!/bin/bash 
echo "Digite o local em que deseja instalar o IGRAFU(pressione enter para 
instalar em 
${HOME}/IGRAFO):"
read caminho
if test -z $caminho
then
caminho="${HOME}/IGRAFO"
fi
echo "O IGRAFU será instalado no diretório: $caminho"
mkdir -pv $caminho

mv -v ./* $caminho
echo "Pronto!"

在"read caminho"中,我可能会从用户那里收到〜/somefolder之类的路径.当脚本收到这种路径时,mv和mkdir都不会进行波浪号扩展,因此它将尝试创建〜/somefolder而不是/home/username/somefolder,因此失败.

At 'read caminho' I may receive from the user a path like ~/somefolder. When the script receives that kind of path both mv and mkdir won't make tilde expansion, so it will try to create a ~/somefolder and not /home/username/somefolder and therefore fail.

如何确保将波浪号转换为HOME变量?

How do I ensure that the tilde will be converted into the HOME variable?

推荐答案

您可能需要评估该变量才能正确替换它.一个例子就是简单地做

You will probably need to eval the variable to have it substituted correctly. One example would be to simply do

caminho=`eval "echo $caminho"`

请记住,如果caminho包含分号或引号,这将中断,并且还将反斜杠转义,并且,如果数据不受信任,则需要注意,您不是注入攻击的目标

Keep in mind that this will break if caminho contains semicolons or quotes, it will also treat backslashes as escaping, and if the data is untrusted, you need to take care that you're not the target of an injection attack.

希望有帮助.

这篇关于Shell脚本波浪号扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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