Bash - 将变量初始化为命令“权限被拒绝"的输出; [英] Bash - Initialize variable to output of command "permission denied"

查看:47
本文介绍了Bash - 将变量初始化为命令“权限被拒绝"的输出;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小的 bash 脚本来从每个文件名中获取数字.例如,文件名 helloworld1.txt 将产生 1.

I'm writing a small bash script to get the number out of each file name. For example the file name helloworld1.txt would produce 1.

当尝试将输出设置为变量 i 时,我收到每个文件的错误.

When attempting to set the output to the variable i I get an error for each file.

line 5: 985.txt: Permission denied

如果我只是回显命令 echo $f |tr -dc '[0-9]' 而不是将其分配给变量,一切都很好.

If I just echo the command echo $f | tr -dc '[0-9]' rather than assign it to a variable, everything is good.

#!/bin/bash

for f in *
do
        i=`$f | tr -dc '[0-9]'` // Permission denied.
        echo $i
done

推荐答案

您在这里错过了 echo.线

You have missed echo here. The line

i=`$f | tr -dc '[0-9]'`

应该

i=`echo $f | tr -dc '[0-9]'`

这篇关于Bash - 将变量初始化为命令“权限被拒绝"的输出;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆