【问题标题】:How to get filename without extension from %f in bash terminal?如何在 bash 终端中从 %f 获取不带扩展名的文件名?
【发布时间】:2014-06-19 11:28:45
【问题描述】:

下面的程序(更改时)给出了文件名%f,可以在命令中使用。我怎样才能从这个%f 只获取没有文件扩展名的文件名?

这是我要使用的命令:

when-changed *.scss -c sassc %f f%-min.css

它保存一个文件名,如:layout.scss-min.css 如果可能的话,我只需要layout-min.css

【问题讨论】:

标签: bash filenames


【解决方案1】:

创建一个为您调用 sassc 的小 shell 脚本,而不是直接调用它:

#!/bin/bash
filename=$1
outfile="${filename%.*}"  # Do your filename replacement here
sassc "$filename" "$outfile"

然后调用:

 when-changed *.scss -c ./myscript.sh %f

【讨论】:

    【解决方案2】:

    您不能在命令行上执行此操作。文件名并没有真正放在命令行的%f 变量中。相反,您将 %f 传递给 when-changed 并且 when-changed 本身将其替换为文件名。

    更改source of when-changed 来做你想做的事。比如改变这部分:

    def run_command(self, file):
        os.system(self.command.replace('%f', file))
    

    到这里:

    def run_command(self, file):
        command = self.command
        command = command.replace('%f', file)
        command = command.replace('%c', file.replace('.scss', ''))
        os.system(command)
    

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 2011-06-15
      • 2011-10-02
      • 1970-01-01
      • 2011-05-25
      • 2015-05-20
      • 1970-01-01
      相关资源
      最近更新 更多