简体   繁体   English

关于UNIX Shell脚本

[英]Regarding UNIX Shell Script

When there is no files inside the folder the below script goes inside the for loop. 如果文件夹内没有文件,则以下脚本进入for循环。 Not sure what i can modify so that it doesn't go inside the for loop. 不知道我可以修改什么,以便它不会进入for循环内。 Also when there is no files inside the directory exit status should be success. 另外,当目录内没有文件时,退出状态应该为成功。 Wrapper script checks the exit status of the below script 包装器脚本检查以下脚本的退出状态

     FILESRAW ="/exp/test1/folder"  .
for fspec in "$FILESRAW"/* ; do
  echo "$fspec"
  if [[ -f ${fspec} ]] ; then
       ..... processing logic
  else
     ... processing logic
  fi
done

if using bash, 如果使用bash,

you can set nullglob 您可以设置nullglob

shopt-s nullglob

if you have hidden files, 如果您有隐藏文件,

shopt -s dotglob

with ksh, 用ksh,

#!/bin/ksh
set -o noglob
for file in /path/*
do
  ....
done
for fspec in `dir $FILESRAW` ; do

To exit if $FILESRAW is empty: 如果$ FILESRAW为空,要退出:

[ $( ls "$FILESRAW" | wc -l ) -eq 0 ] && exit 0

If this test precedes the loop, it will prevent execution from reaching the for loop if $FILESRAW is empty. 如果此测试在循环之前进行,则如果$ FILESRAW为空,它将阻止执行到达for循环。

When $FILESRAW is empty, "$FILESRAW"/* expands to "/exp/test1/folder/*", as ghostdog74 points out, you can change this behavior by setting nullglob with 当$ FILESRAW为空时,如ghostdog74所指出的,“ $ FILESRAW” / *扩展为“ / exp / test1 / folder / *”,您可以通过设置nullglob与

shopt -s nullglob

If you want hidden files, set dotglob as well: 如果要隐藏文件,请同时设置dotglob:

shopt -s dotglob

Alternately, you could use ls instead of globing. 或者,您可以使用ls而不是globing。 This has the advantage of working with very full directories (using a pipe, you won't reach the maximum argument limit): 这具有处理非常完整的目录的优点(使用管道,您将不会达到最大参数限制):

ls "$FILESRAW" | while read file; do
     echo "$file"

This becomes messier if you want hidden files, since you'll need to exclude . 如果您需要隐藏文件,这将变得更加混乱,因为您需要排除。 and .. to emulate globing behavior: 和..模拟全球行为:

ls -a "$FILESRAW" | egrep -v '^(\.|\.\.)$' | while read file; do
    echo "$file"

if you are using ksh, try putting this in front of for loop so that it won't go inside it. 如果您使用的是ksh,请尝试将其放在for循环的前面,以免它进入其中。 " set -noglob " Even I have got the same problem, but I was able to resolve it by doing this. set -noglob ”即使我也遇到了同样的问题,但是我能够通过这样做解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM