为什么这个示例脚本会在令牌附近输出错误?(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

the code as it appears in the book


原文:https://stackoverflow.com/questions/18051283
2024-04-21 20:04

满意答案

如果要解析PHP,可以使用token_get_all来获取给定代码的标记


If you want to parse PHP, you can use token_get_all to get the tokens of the given code.

相关问答

更多

使用正则表达式PHP搜索字符串(Searching a String using Regular Expression PHP)

使用此正则表达式 $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...

抓住重复 - PHP中的字母 - 正则表达式(Catch repeat - letter in PHP - Regular expressions)

正则表达式不是计算字符串中出现次数的最佳选择。 对于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 ...

PHP使用重叠正则表达式替换字符串(PHP replace string using overlapping regular expressions)

这是因为它们不是同时运行,而是循环运行。 在第一次表达之后,你最终会得到<b>a</b>bc所以第二个表达式不再有任何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 <...

使用正则表达式从PHP中解析字符串中的函数(Using Regular Expressions to parse functions from a string in PHP)

如果要解析PHP,可以使用token_get_all来获取给定代码的标记 。 If you want to parse PHP, you can use token_get_all to get the tokens of the given code.

需要一些正则表达式的帮助(php)(Need some help with regular expressions (php))

.*是贪婪的,所以它会匹配尽可能多的字符,你想要的.*? 这将匹配完成匹配所需的最小字符: /(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?)

你不显示任何代码(你应该有),所以我们猜测你如何使用你的正则表达式。 #\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 ...

PHP使用正则表达式将字符串解析为数组(PHP parsing string to array with regular expressions)

我建议使用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))...

用正则表达式编写PHP查询解析器(Writing a PHP query parser with regular expressions)

理论正则表达式不足以进行括号匹配。 理论正则表达式只能处理左递归/右递归规则。 中间递归规则不能用正则表达式表示(例如<exp> -> "(" <exp> ")" )。 然而,编程语言中的正则表达式实现了允许正则表达式超出常规语法功能的功能。 例如,正则表达式中的反向引用允许编写与非上下文语言匹配的正则表达式。 但是,即使使用反向引用,仍然无法使用正则表达式来平衡括号。 由于PCRE库通过子例程调用功能支持递归正则表达式,因此技术上可以使用正则表达式解析这样的表达式。 但是, 除非您自己编写正则表...

如何使用PHP中的正则表达式在字符串中搜索可以按任何顺序排列的子字符串(How to search a string for substrings that can be in any order using regular expressions in PHP)

使用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...

如何使用正则表达式解析此格式(ICAO飞行计划备注)?(How can I use regular expressions to parse this format (ICAO flight plan remarks)?)

您可以使用正向前瞻断言 (\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 gro...

相关文章

更多

syntax error near unexpected token的解决办法

在window下编辑shell文件,如aa.sh,然后在linux下运行此文件时,就会报错 synta ...

ACCESS TOKEN

Access Token 在微信公众平台接口开发中,Access Token占据了一个很重要的地位,相 ...

The connection to adb is down, and a severe error has occured.

启动android模拟器时.有时会报The connection to adb is down, an ...

Script.NET Perl解释器代码已经在GitHub开源发布

Script.NET Perl解释器的代码已经提交到GitHub网站。GitHub项目地址: http ...

微信开发第四篇:获取access_token

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开 ...

eclipse里报:An internal error occurred during:

eclipse里报:An internal error occurred during: Buildi ...

微信公众平台接口开发-接入验证测试token

今天打算开发一个微信公众平台的一个阅读功能,使用开发者模式,第一个件就是要通过验证,就是token的验 ...

[Hadoop] Error: JAVA_HOME is not set

在namenode启动脚本%Hadoop_HOME%/bin/start-dfs.sh的时候发现dat ...

Force.com微信开发系列(四)申请Access Token及自定义菜单之创建菜单

在微信接口开发中,许多服务的使用都离不开Access Token,Access Token相当于打开这 ...

微信开发者-主动请求-实际开发-(3)获取access_token(C#)

“access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。 ...

最新问答

更多

python的访问器方法有哪些

使用方法: class A(object): def foo(self,x): #类实例方法 print "executing foo(%s,%s)"%(self,x) @classmethod def class_foo(cls,x): #类方法 print "executing class_foo(%s,%s)"%(cls,x) @staticmethod def static_foo(x): #静态方法 print "executing static_foo(%s)"%x调用方法: a =

使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)

我认为你必须将两个TableGetway传递给UserTable构造。 你必须改变Module.php看看: public function getServiceConfig() { return array( 'factories' => array( 'User\Model\UserTable' => function($sm) { $userTableGateway = $sm->get('UserTable

透明度错误IE11(Transparency bug IE11)

这是一个渲染错误,由使用透明度触发,使用bootstrap用于在聚焦控件周围放置蓝色光环的box-shadow属性。 可以通过添加以下类覆盖来解决它。 .form-control:hover { -webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,255,1); box-shadow: 0px 0px 4px 0px rgba(0,0,255,1)

linux的基本操作命令。。。

ls 显示目录 mkdir 建立目录 cd 进入目录

响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)

将z-index设置为.main-nav这将解决您的重叠问题 .main-nav { position:relative; z-index:9; } set z-index to .main-nav This will fix your overlaping issue .main-nav { position:relative; z-index:9; }

在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)

这是因为模式规范"a"打开一个文件以便追加,文件指针在末尾。 如果您尝试从此处读取,则由于文件指针位于EOF,因此没有数据。 您应该打开"r+"进行阅读和写作。 如果在写入之前读取整个文件,则在写入更多数据时,文件指针将正确定位以追加。 如果这还不够,请探索ftell()和fseek()函数。 That is because the mode spec "a" opens a file for appending, with the file pointer at the end. If you

NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)

支持空中接口的数据速率是一回事。 在消除协议开销,等待eeprom写入以及所有需要时间的其他内容之后,您看到的数据速率是完全不同的故事。 长话短说,从标签读取或进行对等传输时的实际数据速率峰值约为2.5千字节/秒。 取决于具体的标签或对等技术,它可能比这慢很多。 The supported data-rates of the air-interface are one thing. The data-rate that you see after removing protocol overhe

元素上的盒子阴影行为(box-shadow behaviour on elements)

它看起来像只在Windows上的Chrome的错误。 我在Google Canary (Chrome 63)中也进行了测试,问题依然存在,所以有可能它不会很快修复。 这个问题是由overflow: auto引起的overflow: auto ,在你的情况下,它可以很容易地通过删除或设置为可见(默认)来解决。 但是 ,将鼠标悬停在右侧(顶部和底部)时,会出现滚动条。 一个解决方案可以设置overflow: hidden的身体,所以预期的结果是所需的。 我想指出,这不是一个很好的解决方案,但我建议暂

Laravel检查是否存在记录(Laravel Checking If a Record Exists)

这取决于您是否要以后与用户合作,或仅检查是否存在。 如果要使用用户对象(如果存在): $user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist } 如果你只想检查 if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found

设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)

$scope.getData= function () { var reader = new FileReader(); reader.onload = $('input[type=file]')[0].files; var img = new Image(); img.src =(reader.onload[0].result); img.onload = function() { if(this.width > 640