linux - Get expect 取一个带有空格的变量
问题描述
在我的期望脚本中,我有两个要添加的变量。它们都将成为脚本中的变量。
hello="My Life"
world="is wonderful"
./script.sh $hello $world
在脚本.sh
#!/usr/bin/expect
set timeout 10
match_max 100000000
set first_var [lindex $argv 0]
set second_var [lindex $argv 1]
目前,
first_var="My"
second_var="Life"
当不将它们作为变量传递时,下面的代码按预期工作。
./script.sh 'My Life' 'is wonderful'
我需要知道如何让该脚本接受变量并且仍然忽略其中的空格。
解决方案
您还必须在引号内传递变量;
./script.sh "$hello" "$world"
Bash 扩展的工作方式,这些变量将在脚本运行之前被扩展。就像命令运行一样
./script “我的生活” “很精彩”
参数周围的引号指示外壳不要通过字段分隔符进行拆分。
推荐阅读
- reactjs - 当我使用 withCookies() 时收到警告
- postgresql - Solr 数据库配置:带特殊字符的密码
- background-color - SharePoint 现代体验 - ClientSidePage 部分背景 - 如何着色?
- json - WCf返回数据,在c#中转换成Datatable
- apache - htaccess 301 通过向 URL 添加查询参数来重定向
- excel - 公式与显示结果不一致
- reactjs - 使用流星调用 API 资源
- angular - 如何在 TypeScript 或 Angular 类中使用 formElement.selectionStart 和 formElement.selectionEnd
- report - 如何在 IIS 上构建和托管 jsreport 服务器
- python-3.x - ModuleNotFoundError:尽管下载了硒,但没有名为“硒”的模块消息