prevent在bash脚本通配符 [英] Prevent globbing in a bash script

查看:111
本文介绍了prevent在bash脚本通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个脚本,将选定的文件进行操作。

I am trying to write a script that will operate on selected files.

#!/bin/bash
#ytest
lastArgNo=$#
sPattern=${!lastArgNo}
echo "operating on $sPattern"
#do operation on $sPattern
for sFile in $sPattern do
    #do something with each file
done

如果我开始这个脚本
参数* .JPG我得到

If I start this script with Parameters *.JPG I get

operating on IMG_1282.JPG

这是发现模式*正在处理.JPG也只有这个文件中的最后一个文件。我需要的是在命令行给出的实际文件的模式。先谢谢了。

which is the last file found for pattern *.JPG and only this file is being processed. I need the actual file pattern that is given on the commandline. Thanks in advance.

推荐答案

您不能得到实际的模式:启动脚本之前,外壳已经扩大了。你得到的实际文件作为参数,所以你应该做的是遍历所有的参数:

You cannot get the actual pattern: the shell has already expanded it before launching your script. You do get the actual files as arguments, so what you should be doing is iterating over all the arguments:

#!/bin/bash
echo "operating on $# files"
for file; do
    # do something with each "$file"
done

你在做什么是设置lastArgNo成参数的数量,使用间接变量扩展设置sPattern到最后一个参数的值。如果你做了 ARG = 1; sPattern = $ {!ARG} 您将设置sPattern第一个参数。

What you are doing is setting lastArgNo to the number of args, the using indirect variable expansion to set sPattern to the value of the last argument. If you did arg=1; sPattern=${!arg} you would set sPattern to the first argument.

这篇关于prevent在bash脚本通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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