在 bash 脚本中使用 expr substr 语法错误
我编写了一个小型 bash 脚本来尝试获取字符串的最后 x 个字符。
#!/bin/bash
string="This is my string. I want THIS TEXT"
echo -e "string: $string"
length=${#string}
echo "length: $length"
start=$(($length-9))
echo "start: $start"
text=`expr substr $string $start 9`
echo "text: $text"
exit 0
输出给了我一个“语法错误”:
string: This is my string. I want THIS TEXT
length: 35
start: 26
expr: syntax error
text:
我确信它相当简单,但我无法弄清楚。有人能指出我做错了什么吗?这是在 SunOS 上运行的。
I have written small bash script to try to get the last x characters of a string.
#!/bin/bash
string="This is my string. I want THIS TEXT"
echo -e "string: $string"
length=${#string}
echo "length: $length"
start=$(($length-9))
echo "start: $start"
text=`expr substr $string $start 9`
echo "text: $text"
exit 0
The output is giving me a "syntax error":
string: This is my string. I want THIS TEXT
length: 35
start: 26
expr: syntax error
text:
I'm sure it's fairly simple but I can't figure it out. Can anybody point out what I'm doing wrong? This is being run on SunOS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个怎么样?
其中 -4 是距末尾的偏移量
how about this?
where -4 is your offset from the end