This one is stumping me quite well! Big points for a resolution!
I need to traverse a directory and all levels below it to discover any files or directories that do not match a pattern.
I'm using find command -
find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls
The error -
find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls: No such file or directory
Yes, each of those directories in the find command do indeed exist -
ls -ld /home/user/inbox /home/user/inbox/imaging/
drwxr-xr-x. 5 user grp-ftp-admin 8192 Jan 13 12:45 /home/user/inbox
drwxr-xr-x. 3 user grp-ftp-admin 152 Jan 13 12:21 /home/user/inbox/imaging/
The entire script source -
#!/usr/bin/env bash
function findInvalidArtifacts() {
unset inbox sshCommand exitCode cmdOutput
s ilocal -A VENDOR=(
['FTPBASE']="/home/user"
['FTPSOURCEDIR']="/home/user/inbox/imaging"
['FRIENDLYNAME']="user"
['ENVIRONMENT']="dev"
)
inbox="${VENDOR['FTPBASE']}/inbox"
printf -v sshCommand "find -L %s \( ! -path %s -a ! -path '%s/*' -a ! -path %s -a ! -path '%s/*' \) -ls" \
"${inbox}" "${inbox}" "${inbox}" "${VENDOR['FTPSOURCEDIR']}" "${VENDOR['FTPSOURCEDIR']}"
#cmdOutput=$(ssh ${SSHOPTIONS} ${SSHUSERHOST} "${sshCommand}" 2>&1)
cmdOutput=$("${sshCommand}")
exitCode="${?}"
if (( 0 != $exitCode )); then
printf "FAILED - sshCommand=[%s], exitCode=[%d], cmdOutput=[%s]\n" "${sshCommand}" "${exitCode}" "${cmdOutput}"
exit 1
fi
printf "SUCCEEDED - sshCommand=[%s] exitCode=[%d]\n" "${sshCommand}" "${exitCode}"
printf "%s\n\n" "${cmdOutput}"
} # end findInvalidArtifacts()
And, finally, running the script with bash -vx -
#!/usr/bin/env bash
function findInvalidArtifacts() {
unset inbox sshCommand exitCode cmdOutput
local -A VENDOR=(
['FTPBASE']="/home/user"
['FTPSOURCEDIR']="/home/user/inbox/imaging"
['FRIENDLYNAME']="user"
['ENVIRONMENT']="dev"
)
inbox="${VENDOR['FTPBASE']}/inbox"
printf -v sshCommand "find -L %s \( ! -path %s -a ! -path '%s/*' -a ! -path %s -a ! -path '%s/*' \) -ls" \
"${inbox}" "${inbox}" "${inbox}" "${VENDOR['FTPSOURCEDIR']}" "${VENDOR['FTPSOURCEDIR']}"
#cmdOutput=$(ssh ${SSHOPTIONS} ${SSHUSERHOST} "${sshCommand}" 2>&1)
cmdOutput=$("${sshCommand}")
exitCode="${?}"
if (( 0 != $exitCode )); then
printf "FAILED - sshCommand=[%s], exitCode=[%d], cmdOutput=[%s]\n" "${sshCommand}" "${exitCode}" "${cmdOutput}"
exit 1
fi
printf "SUCCEEDED - sshCommand=[%s] exitCode=[%d]\n" "${sshCommand}" "${exitCode}"
printf "%s\n\n" "${cmdOutput}"
} # end findInvalidArtifacts()
findInvalidArtifacts
+ findInvalidArtifacts
+ unset inbox sshCommand exitCode cmdOutput
+ VENDOR=(['FTPBASE']="/home/user" ['FTPSOURCEDIR']="/home/user/inbox/imaging" ['FRIENDLYNAME']="user" ['ENVIRONMENT']="dev")
+ local -A VENDOR
+ inbox=/home/user/inbox
+ printf -v sshCommand 'find -L %s \( ! -path %s -a ! -path '\''%s/*'\'' -a ! -path %s -a ! -path '\''%s/*'\'' \) -ls' /home/user/inbox /home/user/inbox /home/user/inbox /home/user/inbox/imaging /home/user/inbox/imaging
++ 'find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '\''/home/user/inbox/*'\'' -a ! -path /home/user/inbox/imaging -a ! -path '\''/home/user/inbox/imaging/*'\'' \) -ls'
./try.sh: line 16: find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls: No such file or directory
+ cmdOutput=
+ exitCode=127
+ (( 0 != 127 ))
+ printf 'FAILED - sshCommand=[%s], exitCode=[%d], cmdOutput=[%s]\n' 'find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '\''/home/user/inbox/*'\'' -a ! -path /home/user/inbox/imaging -a ! -path '\''/home/user/inbox/imaging/*'\'' \) -ls' 127 ''
FAILED - sshCommand=[find -L /home/user/inbox \( ! -path /home/user/inbox -a ! -path '/home/user/inbox/*' -a ! -path /home/user/inbox/imaging -a ! -path '/home/user/inbox/imaging/*' \) -ls], exitCode=[127], cmdOutput=[]
+ exit 1
This is a stumper. I cannot see where why find is complaining about 'no such file or directory'.
Appreciate the valiant effort, however the constructed command still does not return the desired results. The contents of finding all existing artifacts in /home/user/inbox are:
/home/user/inbox
/home/user/inbox/invalidDir2
/home/user/inbox/invalidDir2/invalidSubdir2
/home/user/inbox/invalidDir2/invalidSubdir2/invalidFile1
/home/user/inbox/invalidDir2/invalidSubdir2/invalidSubSubDir2
/home/user/inbox/invalidDir2/invalidSubdir2/invalidSubSubDir2/invalidSubSubSubDir2
/home/user/inbox/invalidDir2/invalidSubdir2/invalidSubSubDir2/invalidSubSubSubDir2/invalidSubSubSubSubDir2
/home/user/inbox/invalidDir2/invalidSubdir2/invalidSubSubDir2/invalidSubSubSubDir2/invalidSubSubSubSubDir2/invalidFile
/home/user/inbox/invalidDir2/invalidSubdir2/invalidSubSubDir2/invalidSubSubSubDir2/invalidFile
/home/user/inbox/invalidDir2/invalidSubdir2/invalidSubSubDir2/invalidFile
/home/user/inbox/imaging
/home/user/inbox/imaging/validFile
/home/user/inbox/imaging/invalidFile
/home/user/inbox/imaging/invalidImagingDir1
/home/user/inbox/imaging/invalidImagingDir1/invalidSubdir1
/home/user/inbox/imaging/invalidImagingDir1/invalidSubdir1/invalidFile1
/home/user/inbox/imaging/invalidImagingDir1/invalidSubdir1/invalidSubSubDir1
/home/user/inbox/imaging/invalidImagingDir1/invalidSubdir1/invalidSubSubDir1/invalidSubSubSubDir1
/home/user/inbox/imaging/invalidImagingDir1/invalidSubdir1/invalidSubSubDir1/invalidSubSubSubDir1/invalidFile
/home/user/inbox/imaging/invalidImagingDir1/invalidSubdir1/invalidSubSubDir1/invalidFile
/home/user/inbox/imaging/invalidImagingDir1/invalildFile
/home/user/inbox/invalidFile
/home/user/inbox/invalidDir1
/home/user/inbox/invalidDir1/invalidSubdir1
/home/user/inbox/invalidDir1/invalidSubdir1/invalidFile1
/home/user/inbox/invalidDir1/invalidSubdir1/invalidSubSubDir1
/home/user/inbox/invalidDir1/invalidSubdir1/invalidSubSubDir1/invalidSubSubSubDir1
/home/user/inbox/invalidDir1/invalidSubdir1/invalidSubSubDir1/invalidSubSubSubDir1/invalidFile
/home/user/inbox/invalidDir1/invalidSubdir1/invalidSubSubDir1/invalidFile
The result set should NOT contain anything matching the directory to search in, or any of the parameters passed to your function that constructs the command. Results should NOT contain these lines -
/home/user/inbox
/home/user/inbox/imaging
/home/user/inbox/imaging/validFile
The command your function constructs looks like this -
find -L /home/user/inbox '(' '!' -path /home/user/inbox -a '!' -path '/home/user/inbox/*' -a '!' -path /home/user/inbox/imaging -a '!' -path '/home/user/inbox/imaging/*' ')' -ls
which returns nothing at all
I'm beginning to think that the find command is not what I need to obtain the desired results and that I'll need to do some more processing of the result set to disclude, probably awk or something.
Relevant snippet:
printf -v sshCommand "find -L %s \( ! -path %s -a ! -path '%s/*' -a ! -path %s -a ! -path '%s/*' \) -ls" [more args]
cmdOutput=$("${sshCommand}")
This is a classic pitfall of trying to shove a command into a variable and run it. The basic solution is to use a function, not a variable.
The reason for your error is that Bash determines the name of the command you're trying to run by selecting the first word of the command line after completing all of its parsing steps. You want to use the command find with some arguments, but since you have "${sshCommand}" in quotes, there's only one word in the entire command line so Bash thinks the command you want is called find -L /home/dir ( ! -path etc etc... ). I bet you don't have any executables with that name on your system.
A function equivalent of what you're trying to do might look like:
_findCmd() {
searchDir=$1 # first arg is the directory to search from
shift
# each extra argument is a directory to exclude from the search
# we can loop over the list and build the arguments that find needs
# in an array
path_args=()
if (( $# > 0 )) ; then
path_args+=( "(" "!" "-path" "$1" )
shift
while (( $# > 0 )) ; do
path_args+=( "-a" "!" "-path" "$1" )
shift
done
path_args+=( ")" )
fi
find -L "$searchDir" "${path_args[#]}" -ls
}
cmdOutput=$( _findCmd /home/user/inbox /home/user/inbox "/home/user/inbox/*" /home/user/inbox/imaging "/home/user/inbox/imaging/*" )
Related
#!/bin/bash
dir=$1
cd $dir
list=$(find . -maxdepth 1 -mindepth 1 -type d -printf '%f,')
IFS=',' read -a array <<< "${list}"
for i in "${array[#]}"
do
longPermission=$(getfacl $i |stat -c %A $i)
longOwner=$(getfacl $i |grep owner)
ownerPermission=${longPermission:1:3}
owner=${longOwner:9}
if (($ownerPermission=="rwx"))
then
permission='FULL_PERMISSION'
fi
if (($ownerPermission=="rw-"))
then
permission='READ/WRITE_PERMISSION'
fi
#if (($ownerPermission=="r--"))
#then
# permission='READ_ONLY_PERMISSION'
#fi
echo ’BUERO\ $owner -user -group -type windows permission $permission’
done
Bash treats the "-" in rw- and rw-- as arithmetic operators but what I wanto to achieve is processing it as string because I want to work with output I get from getfacl $i |grep owner which gives me back a string I wanna echo in
echo ’BUERO\ $owner -user -group -type windows permission $permission’
thanks in advance!
Use if [[ $ownerPermission == "rwx" ]] and if [[ $ownerPermission == "rw-" ]]
The (( expr )) is used for arithemtic expressions, which you don't need here.
I have created a script to zip and move log files from one directory to another directory to free space. This is the script:
#!/bin/bash
logsDirectory="/test//logs/"
email=""
backupDirectory="/test/backup"
pid="/data/test/scripts/backup.pid"
usage=$(df | grep /data/logs | awk '{ print $2 }')
space=450000000
getBackup ()
{
if [[ ! -e $pid ]] then
if [[ $usage -le $space ]]
then
touch $pid
find $backupDirectory -mtime +15 -type f -delete;
for i in $(find $logsDirectory -type f -not -path "*/irws/*")
do
/sbin/fuser $i > /dev/null 2>&1
if [ $? -ne 0 ]
then
gzip $i
mv -v $i.gz $backupDirectory
else
continue
fi
done
[[ ! -z $email ]] && echo "Backup is ready" | mas"Backup" $email
rm -f $pid
fi
fi
}
getBackup
I am getting this error:
gzip: /data/logs/log01.log.gz already has .gz suffix -- unchanged
mv: cannot stat `/data/logs/log01.log.gz': No such file or directory
I got the error every time I ran the script in my DEV and PROD (CentOS servers) environments. To analyse it, I ran the same script in a VM (Ubuntu) in my laptop, and I don't get the error there.
My questions:
How can I prevent this error?
What I have done wrong in the script?
Your script contains a number of common clumsy or inefficient antipatterns. Here is a refactoring. The only real change is skipping any *.gz files.
#!/bin/bash
logsDirectory="/test//logs/"
email=""
backupDirectory="/test/backup"
pid="/data/test/scripts/backup.pid"
# Avoid useless use of grep -- awk knows how to match a regex
# Better still run df /data/logs
usage=$(df /data/logs/ | awk '{ print $2 }')
space=450000000
getBackup ()
{
# Quote variables
if [[ ! -e "$pid" ]]; then
if [[ "$usage" -le "$space" ]]; then
touch "$pid"
find "$backupDirectory" -mtime +15 -type f -delete;
# Exclude *.gz files
# This is still not robust against file names with spaces or wildcards in their names
for i in $(find "$logsDirectory" -type f -not -path "*/irws/*" -not -name '*.gz')
do
# Avoid useless use of $?
if /sbin/fuser "$i" > /dev/null 2>&1
then
gzip "$i"
mv -v "$i.gz" "$backupDirectory"
# no need for do-nothing else
fi
done
[[ ! -z "$email" ]] &&
echo "Backup is ready" | mas"Backup" "$email"
rm -f "$pid"
fi
fi
}
getBackup
With a slightly more intrusive refactoring, the proper fix to the find loop would perhaps look something like
find "$logsDirectory" -type f \
-not -path "*/irws/*" -not -name '*.gz' \
-exec sh -c '
for i; do
if /sbin/fuser "$i" > /dev/null 2>&1
then
gzip "$i"
mv -v "$i.gz" "$backupDirectory"
fi
done' _ {} +
where the secret sauce is to have find ... -exec + pass in the arguments to the sh -c script in a way which does not involve exposing the arguments to the current shell at all.
What I have done wrong in the script?
Your script tries to zip every file but the gzip command is rejecting files already zipped
How can I prevent this error?
Have the script check whether the file is zipped or not and only gzip if it corresponds (1). Alternatively, you could force re-compression even if it is already compressed (2).
Going with option number 1):
getBackup ()
{
if [[ ! -e $pid ]] then
if [[ $usage -le $space ]]
then
touch $pid
find $backupDirectory -mtime +15 -type f -delete;
for i in $(find $logsDirectory -type f -not -path "*/irws/*")
do
/sbin/fuser $i > /dev/null 2>&1
if [ $? -ne 0 ]
then
if [[ $i =~ \.gz$ ]]
# File is already zipped
mv -v $i $backupDirectory
else
gzip $i
mv -v $i.gz $backupDirectory
fi
else
continue
fi
done
[[ ! -z $email ]] && echo "Backup is ready" | mas"Backup" $email
rm -f $pid
fi
fi
}
i'm having some trouble with a bash script i'm currently writing, and I've isolated the problematic code.
Find command when using argument from variable does not exclude directories.
Now i need to assign the '-not -path ./dir' argument to a variable as it's being looped over, however if i do, it doesn't seem to work.
I've included below the command that produces the desired output, along with my version which currently is broken. Please see my test below:
Variable assignment
findIgnorePaths="-not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*"
Desired output.
find ./ -type d -not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*
Broken output
find ./ -type d ${findIgnorePaths}
And for anyone who wants to see my script i'm working on which the problem above resides in, then please take a look below:
Original script (Not required to read to answer question, but... provided for those curious souls)
#!/bin/sh
declare -a ignoreDirs=()
findIgnorePaths=""
seperator="\e[33m**********************************\e[0m\n"
clear
printf "\n\n"
if [[ "${PWD##*/}" != "public_html" ]]
then
printf "\e[31mThis script will not run unless current location is; /home/*/public_html/*\e[0m\n"
exit
fi
echo "Site Locker! This will change all directory permissions to 555. And all file permissions to 444. Read only accces."
echo #Move to new line
echo "Are you sure you want to make changes to files/directories within the below location?"
printf "$seperator"
printf "\e[0;49;96m${PWD##/home/}\e[0m\n"
printf "$seperator"
echo #Move to new line
read -r -p "Y/n " response
echo #Move to new line
if [[ $response =~ ^([nN][oO]|[nN])$ ]]
then
printf "\n\e[31mNo changes made. Quitting.\e[0m\n\n"
exit
fi
printf "\e[95mIf you're working with a Joomla site, please select\nthe **JOOMLA** preset in the list below.\e[0m\n\nPlease select directory #) to add to the ignore list:\n\e[92m"
currentDir=( "**SAVE/SKIP**" "**JOOMLA**" )
currentDir+=( $(find . -maxdepth 1 -type d) )
select DIR in ${currentDir[#]};
do
case $DIR in
*SAVE*)
printf "\n\n\e[92mDone!!\e[0m\n\n"
break
;;
*JOOMLA*)
printf "\n\e[92mApplying Joomla preset.\n"
ignoreDirs+=("./.git" "./administrator/cache" "./cache" "./images" "./logs" "./media" "./tmp" "./plugins")
findIgnorePaths+=" -not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*"
printf "\n\n\e[31mIgnore list:\n"
for item in "${ignoreDirs[#]}"
do
printf "$item\n"
done
;;
*)
ignoreDirs+=("$DIR")
findIgnorePaths+=" -not -path ${DIR}\*"
printf "\n\n\e[31mIgnore list:\n"
for item in "${ignoreDirs[#]}"
do
printf "$item\n"
done
printf "\e[92m"
;;
esac
done
findIgnorePaths=$(echo ${findIgnorePaths} | cut -c 1-)
printf "\e[0m\n"
echo #Move to new line
printf "$seperator"
echo #Move to new line
echo "Apply 555 permissions to the below directories & all sub directories? "
printf "$seperator"
printf "\e[0;49;96m"
echo
echo "$findIgnorePaths"
echo
find ./ -maxdepth 1 -type d -not -path ./\.git\* -not -path ./administrator/cache\* -not -path ./images\* -not -path ./logs\* -not -path ./cache\* -not -path ./media\* -not -path ./plugins\* -not -path ./tmp\*
echo
find ./ -maxdepth 1 -type d ${findIgnorePaths}
echo
printf "\e[0m\n"
printf "$seperator"
read -r -p "Y/n " response
echo #Move to new line
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
find ./ -type d $findIgnorePaths -exec chmod 555 {} +
printf "\e[92mChanged directory permissions to 555\e[0m\n\n"
else
printf "\e[31mSkipping this step. No directory permissions were set.\e[0m\n\n"
fi
read -r -p "Apply 444 permissions to all files in; ${PWD##*/}? Y/n " response
echo #Move to new line
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
find ./ -type f $findIgnorePaths -exec chmod 444 {} +
printf "\e[92mChanged file permissions to 444\e[0m\n\n"
else
printf "\e[31mSkipping this step. No file permissions were set.\e[0m\n\n"
fi
printf "\n\e[92mFinished!\e[0m\n\n"
This is Bash FAQ 50, which advises you to use an array variable to pass parameters to programs:
findIgnorePaths=(-not -path './.git*' -not -path './administrator/cache*' -not -path './images*' -not -path './logs*' -not -path './cache*' -not -path './media*' -not -path './plugins*' -not -path './tmp*')
find ./ -type d "${findIgnorePaths[#]}"
Note directory globs are single quoted so they aren't interpreted by the shell when the variable is created. Your backslash escapes were doing the same thing; it's a matter of personal preference which one you find easier to read.
Running scripts through shellcheck.net is always a good idea to find any problems that might be lurking.
Also, /bin/sh is not /bin/bash, so fix your shebang!
I want to use a find command in order to obtain files older than 8640 min and send the result in a email body. I used this script that makes use of a file - ATTACH_FILE - containing the results of the find command:
#!/bin/sh
ATTACH_FILE="/pub/email_attach.txt"
WORK_DIR="/pub/"
rm -f $ATTACH_FILE
find $WORK_DIR -maxdepth 1 -name '*x.rsd' -type f -daystart -mmin +8640 -exec echo {} >> $ATTACH_FILE \;
if [ ! -z $ATTACH_FILE ]; then
FILESIZE=$(stat -c%s "$ATTACH_FILE" 2>> getLatestErr.log)
echo $ATTACH_FILE "size $FILESIZE bytes"
if [ $FILESIZE -gt 0 ]; then
cat $ATTACH_FILE | mail -s "Test "$TODAY mmm#server.com
fi
fi
How can I get the same result by putting a message in the body of the email without using the auxiliary file ATTACH_FILE ?
You can use the -e option to mail. That tells it not to do anything if the input is empty.
find $WORK_DIR -maxdepth 1 -name '*x.rsd' -type f -daystart -mmin +8640 -print | mail -e -s "Test "$TODAY mmm#server.com
To expand on my comment above:
Assign to an array variable and use printf to separate the found items with a newline character:
#!/bin/bash
WORK_DIR="/pub/"
FILE_LIST=($(find $WORK_DIR -maxdepth 1 \
-name '*x.rsd' -type f \
-daystart -mmin +8640 ))
if [ -n "${FILE_LIST[0]}" ]; then
printf '%s\n' "${FILE_LIST[#]}" | mail -s "Test "$TODAY mmm#server.com
fi
I exchanged /bin/sh with /bin/bash, as the question is tagged with [bash].
I am trying to create a script that will find all the files in a folder that contain, for example, the string 'J34567' and process them. Right now I can process all the files in the folder with my code, however, my script will not just process the contained string it will process all the files in the folder. In other words once I run the script even with the string name ./bashexample 'J37264' it will still process all the files even without that string name. Here is my code below:
#!/bin/bash
directory=$(cd `dirname .` && pwd)
tag=$1
echo find: $tag on $directory
find $directory . -type f -exec grep -sl "$tag" {} \;
for files in $directory/*$tag*
do
for i in *.std
do
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < $i > $i.sum
done
for j in *.txt
do
egrep "device|Device|\(F\)" $i > $i.fail
done
echo $files
done
Kevin, you could try the following:
#!/bin/bash
directory='/home'
tag=$1
for files in $directory/*$tag*
do
if [ -f $files ]
then
#do your stuff
echo $files
fi
done
where directory is your directory name (you could pass it as a command-line argument too) and tag is the search term you are looking for in a filename.
Following script will give you the list of files that contain (inside the file, not in file name) the given pattern.
#!/bin/bash
directory=`pwd`
tag=$1
for file in $(find "$directory" -type f -exec grep -l "$tag" {} \;); do
echo $file
# use $file for further operations
done
What is the relevance of .std, .txt, .sum and .fail files to the files containing given pattern?
Its assumed there are no special characters, spaces, etc. in file names.
If that is the case following should help working around those.
How can I escape white space in a bash loop list?
Capturing output of find . -print0 into a bash array
There are multiple issues in your script.
Following is not required to set the operating directory to current directory.
directory=$(cd `dirname .` && pwd)
find is executed twice for the current directory due to $directory and ..
find $directory . -type f -exec grep -sl "$tag" {} \;
Also, result/output of above find is not used in for loop.
For loop is run for files in the $directory (sub directories not considered) with their file name having the given pattern.
for files in $directory/*$tag*
Following for loop will run for all .txt files in current directory, but will result in only one output file due to use of $i from previous loop.
for j in *.txt
do
egrep "device|Device|\(F\)" $i > $i.fail
done
This is my temporary solution. Please check if it follows your intention.
#!/bin/bash
directory=$(cd `dirname .` && pwd) ## Should this be just directory=$PWD ?
tag=$1
echo "find: $tag on $directory"
find "$directory" . -type f -exec grep -sl "$tag" {} \; ## Shouldn't you add -maxdepth 1 ? Are the files listed here the one that should be processed in the loop below instead?
for file in "$directory"/*"$tag"*; do
if [[ $file == *.std ]]; then
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$file" > "${file}.sum"
fi
if [[ $file == *.txt ]]; then
egrep "device|Device|\(F\)" "$file" > "${file}.fail"
fi
echo "$file"
done
Update 1
#!/bin/bash
directory=$PWD ## Change this to another directory if needed.
tag=$1
echo "find: $tag on $directory"
while IFS= read -rd $'\0' file; do
echo "$file"
case "$file" in
*.std)
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$file" > "${file}.sum"
;;
*.txt)
egrep "device|Device|\(F\)" "$file" > "${file}.fail"
;;
*)
echo "Unexpected match: $file"
;;
esac
done < <(exec find "$directory" -maxdepth 1 -type f -name "*${tag}*" \( -name '*.std' -or -name '*.txt' \) -print0) ## Change or remove the maxdepth option as wanted.
Update 2
#!/bin/bash
directory=$PWD
tag=$1
echo "find: $tag on $directory"
while IFS= read -rd $'\0' file; do
echo "$file"
/projects/OPSLIB/BCMTOOLS/sumfmt_linux < "$file" > "${file}.sum"
done < <(exec find "$directory" . -maxdepth 1 -type f -name "*${tag}*" -name '*.std' -print0)
while IFS= read -rd $'\0' file; do
echo "$file"
egrep "device|Device|\(F\)" "$file" > "${file}.fail"
done < <(exec find "$directory" -maxdepth 1 -type f -name "*${tag}*" -name '*.txt' -print0)