为什么这个示例脚本会在令牌附近输出错误?(Why do this sample script, keep outputting error near token?)
在这里输入图像描述我试图看看shell脚本是如何工作的以及如何运行它们,所以我从一本名为“Wicked Cool Shell Scripts”的图书库中找到的一本书中提取了一些示例代码。
我逐字写了代码,但是我从Linux得到了一个错误,我在编译代码时说:
'd.sh:第3行:意外令牌附近的语法错误`{
'd.sh:第3行:`gmk(){
在此之前,我在换行符上有大括号,但我仍然得到:
'd.sh:第3行:意外令牌附近的语法错误
'd.sh:第3行:`gmk()
#!/bin/sh #format directory- outputs a formatted directory listing gmk() { #Give input in Kb, output converted to Kb, Mb, or Gb for best output format if [$1 -ge 1000000]; then echo "$(scriptbc -p 2 $1/1000000)Gb" elif [$1 - ge 1000]; then echo "$$(scriptbc -p 2 $1/1000)Mb" else echo "${1}Kb" fi } if [$# -gt 1] ; then echo "Usage: $0 [dirname]" >&2; exit 1 elif [$# -eq 1] ; then cd "$@" fi for file in * do if [-d "$file"] ; then size = $(ls "$file"|wc -l|sed 's/[^[:digit:]]//g') elif [$size -eq 1] ; then echo "$file ($size entry)|" else echo "$file ($size entries)|" fi else size ="$(ls -sk "$file" | awk '{print $1}')" echo "$file ($(gmk $size))|" fi done | \ sed 's/ /^^^/g' |\ xargs -n 2 |\ sed 's/\^\^\^/ /g' | \ awk -F\| '{ printf "%39s %-39s\n", $1, $2}' exit 0 if [$#-gt 1]; then echo "Usage :$0 [dirname]" >&2; exit 1 elif [$# -eq 1]; then cd "$@" fi for file in * do if [ -d "$file" ] ; then size =$(ls "$file" | wc -l | sed 's/[^[:digit:]]//g') if [ $size -eq 1 ] ; then echo "$file ($size entry)|" else echo "$file ($size entries)|" fi else size ="$(ls -sk "$file" | awk '{print $1}')" echo "$file ($(convert $size))|" fi done | \ sed 's/ /^^^/g' | \ xargs -n 2 | \ sed 's/\^\^\^/ /g' | \ awk -F\| '{ printf "%-39s %-39s\n", $1, $2 }' exit 0
enter image description hereI was trying to see how a shell scripts work and how to run them, so I toke some sample code from a book I picked up from the library called "Wicked Cool Shell Scripts"
I re wrote the code verbatim, but I'm getting an error from Linux, which I compiled the code on saying:
'd.sh: line 3: syntax error near unexpected token `{
'd.sh: line 3:`gmk() {
Before this I had the curly bracket on the newline but I was still getting :
'd.sh: line 3: syntax error near unexpected token
'd.sh: line 3:`gmk()
#!/bin/sh #format directory- outputs a formatted directory listing gmk() { #Give input in Kb, output converted to Kb, Mb, or Gb for best output format if [$1 -ge 1000000]; then echo "$(scriptbc -p 2 $1/1000000)Gb" elif [$1 - ge 1000]; then echo "$$(scriptbc -p 2 $1/1000)Mb" else echo "${1}Kb" fi } if [$# -gt 1] ; then echo "Usage: $0 [dirname]" >&2; exit 1 elif [$# -eq 1] ; then cd "$@" fi for file in * do if [-d "$file"] ; then size = $(ls "$file"|wc -l|sed 's/[^[:digit:]]//g') elif [$size -eq 1] ; then echo "$file ($size entry)|" else echo "$file ($size entries)|" fi else size ="$(ls -sk "$file" | awk '{print $1}')" echo "$file ($(gmk $size))|" fi done | \ sed 's/ /^^^/g' |\ xargs -n 2 |\ sed 's/\^\^\^/ /g' | \ awk -F\| '{ printf "%39s %-39s\n", $1, $2}' exit 0 if [$#-gt 1]; then echo "Usage :$0 [dirname]" >&2; exit 1 elif [$# -eq 1]; then cd "$@" fi for file in * do if [ -d "$file" ] ; then size =$(ls "$file" | wc -l | sed 's/[^[:digit:]]//g') if [ $size -eq 1 ] ; then echo "$file ($size entry)|" else echo "$file ($size entries)|" fi else size ="$(ls -sk "$file" | awk '{print $1}')" echo "$file ($(convert $size))|" fi done | \ sed 's/ /^^^/g' | \ xargs -n 2 | \ sed 's/\^\^\^/ /g' | \ awk -F\| '{ printf "%-39s %-39s\n", $1, $2 }' exit 0
原文:https://stackoverflow.com/questions/18051283
最满意答案
如果要解析PHP,可以使用
token_get_all
来获取给定代码的标记 。If you want to parse PHP, you can use
token_get_all
to get the tokens of the given code.
相关问答
更多-
使用此正则表达式 $str='infoone:"infoone"infotwo:"infotwo"address:"123 fake street pretend land"infothree:"infothree"infofour:"infofour" address:"345 fake street pretend land"infofive: "infofive"infosix: "infosix"'; preg_match_all("/address:\"(.*)\"/siU",$str,$out ...
-
正则表达式不是计算字符串中出现次数的最佳选择。 对于PHP,存在一个名为substr_count()的方法 例如 $s = 'stack-overflow - 2014 - new'; if (substr_count($s, '-') >= 2) { // at least 2 times } Regex is not the best choice to count the occurrence in a string. For PHP, there exists a method calle ...
-
这是因为它们不是同时运行,而是循环运行。 在第一次表达之后,你最终会得到abc所以第二个表达式不再有任何abc匹配。 就是这样。 这很好,因为谢谢你 不会以无效标记结束 。 你会。 正则表达式是HTML或XML的严重问题。 使用一些DOM解释器和库,比如PHP的DOMDocument It's because they don't run simultaneously, but in loop. And after first expression you end up with a ...
-
使用正则表达式从PHP中解析字符串中的函数(Using Regular Expressions to parse functions from a string in PHP)[2021-08-21]
如果要解析PHP,可以使用token_get_all来获取给定代码的标记 。 If you want to parse PHP, you can use token_get_all to get the tokens of the given code. -
.*是贪婪的,所以它会匹配尽可能多的字符,你想要的.*? 这将匹配完成匹配所需的最小字符: /(value).*?(\(\d+\))/ .* is greedy, so it will match as many characters as possible, you want .*? which will match the minimum characters needed to complete the match: /(value).*?(\(\d+\))/
-
有没有比使用正则表达式更快地解析主题标签的方法?(Is there a faster way to parse hashtags than using Regular Expressions?)[2022-01-29]
你不显示任何代码(你应该有),所以我们猜测你如何使用你的正则表达式。 #\S+与您需要的模式一样好,但scan可能是检索字符串中所有匹配项的最佳方法。 'This is a #hashtag, and this is #another one!'.scan(/#\S+/) => ["#hashtag,", "#another"] 它应该是/ \ B#\ w + /,如果你不想分析逗号 是的我同意。 /\B#\w+/更有意义。 You don't show any code (which you shoul ... -
我建议使用preg_split和以下正则表达式: $re = "/([a-z]*(?:\\[[^]]*\\]|\\([^()]*\\)),?)|(?<=,)/"; $str = "\$msg,array('goo','gle'),000,\"face\",'book',['twi'=>'ter','link'=>'edin']"; print_r(preg_split($re, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)); 示例 ...
-
理论正则表达式不足以进行括号匹配。 理论正则表达式只能处理左递归/右递归规则。 中间递归规则不能用正则表达式表示(例如
-> "(" ")" )。 然而,编程语言中的正则表达式实现了允许正则表达式超出常规语法功能的功能。 例如,正则表达式中的反向引用允许编写与非上下文语言匹配的正则表达式。 但是,即使使用反向引用,仍然无法使用正则表达式来平衡括号。 由于PCRE库通过子例程调用功能支持递归正则表达式,因此技术上可以使用正则表达式解析这样的表达式。 但是, 除非您自己编写正则表达式 ,这 ... -
使用json_decode()访问每个项目应该很容易 $results = json_decode('{"ID":2958,"Label":"Reprise Records","Name":"Electric Ladyland","Year":1968}', true); print_r($results); 输出:数组([ID] => 2958 [标签] => Reprise记录[名称] =>电子Ladyland [年] => 1968) It should be easy to access ever ...
-
您可以使用正向前瞻断言 (\w+)\/([^\/]+?)(?= \w+\/|$) Debuggex演示 Regex101演示 然后枚举所有匹配,对于每个匹配组1将是指示符,组2将是内容。 You can use positive lookahead assertion (\w+)\/([^\/]+?)(?= \w+\/|$) Debuggex Demo Regex101 Demo Then enumerate through all matches and for each match group 1 ...