首页 \ 问答 \ 为什么这个示例脚本会在令牌附近输出错误?(Why do this sample script, keep outputting error near token?)

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

相关问答

更多

相关文章

更多

最新问答

更多
  • 带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
  • 高考完可以去做些什么?注意什么?
  • Allauth不会保存其他字段(Allauth will not save additional fields)
  • Flask中的自定义中止映射/异常(Custom abort mapping/exceptions in Flask)
  • sed没有按预期工作,从字符串中间删除特殊字符(sed not working as expected, removing special character from middle of string)
  • 怎么在《我的世界》游戏里面编程
  • .NET可移植可执行文件VS .NET程序集(.NET Portable Executable File VS .NET Assembly)
  • 搜索字符串从视图中键入两个字段的“名字”和“姓氏”组合(Search Strings Typed from View for Two Fields 'First Name' and 'Last Name' Combined)
  • 我可以通过配置切换.Net缓存提供程序(Can I switch out .Net cache provider through configuration)
  • 在鼠标悬停或调整浏览器大小之前,内容不会加载(Content Does Not Load Until Mouse Hover or Resizing Browser)
  • 未捕获的TypeError:auth.get不是函数(Uncaught TypeError: auth.get is not a function)
  • 如何使用变量值创建参数类(How to create a parameter class with variant value)
  • 在std :: deque上并行化std :: replace(Parallelizing std::replace on std::deque)
  • 单元测试返回Connection对象的方法(Unit Test for a method that returns a Connection object)
  • rails:上传图片时ios中的服务器内部错误(rails: server internal error in ios while uploading image)
  • 如何在Android中构建应用程序警报[关闭](How build an application Alarm in Android [closed])
  • 以编程方式连接到Windows Mobile上的蓝牙耳机(Programmatically connect to bluetooth headsets on Windows Mobile)
  • 在两个不同的SharedPreference中编写并获得相同的结果(Writing in two different SharedPreference and getting the same result)
  • CSS修复容器和溢出元素(CSS Fix container and overflow elements)
  • 在'x','y','z'迭代上追加数组(Append array on 'x', 'y', 'z' iteration)
  • 我在哪里可以看到使用c ++源代码的UML方案示例[关闭](Where I can see examples of UML schemes with c++ source [closed])
  • SQL多个连接在与where子句相同的表上(SQL Multiple Joins on same table with where clause)
  • 位字段并集的大小,其成员数多于其大小(Size of bit-field union which has more members than its size)
  • 我安装了熊猫,但它不起作用(I installed pandas but it is not working)
  • Composer - 更改它在env中使用的PHP版本(Composer - Changing the version of PHP it uses in the env)
  • 使用JavaFX和Event获取鼠标位置(Getting a mouse position with JavaFX and Event)
  • 函数调用可以重新排序(Can function calls be reordered)
  • 关于“一对多”关系的NoSQL数据建模(NoSQL Data Modeling about “one to many” relationships)
  • 如何解释SBT错误消息(How to interpret SBT error messages)
  • 调试模式下的Sqlite编译器错误“初始化程序不是常量”(Sqlite compiler errors in Debug mode “initializer is not a constant”)