#!/bin/bash
RESULT=$(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
for i in $(RESULT);
do
echo "$i"
FILENAME="$(dirname $RESULT)"
done
I have a problem with the line FILENAME="$(dirname $RESULT)". Running the script in debugging mode(bash -x script-name), the ouput is:
test.sh: line 9: RESULT: command not found
For some reason, it can't take the result of the variable RESULT and save the output of dir command to the new variable FILENAME. I can't understand why this happens.
After lots of tries, I found the solution to save full path of finame and finame to two different variables.
Now, I want for each finame, find non-case sensitive of each filename. For example, looking for file image.png, it doesn't matter if the file is image.PNG
I am running the script
while read -r name; do
echo "$name"
FILENAME="$(dirname $name)"
BASENAME="$(basename $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
and then enter the command:
find . $FILENAME -iname $BASENAME
but it says command FILENAME and BASENAME not found.
The syntax:
$(RESULT)
denotes command substitution. Saying so would attempt to run the command RESULT.
In order to substitute the result of the variable RESULT, say:
${RESULT}
instead.
Moreover, if the command returns more than one line of output this approach wouldn't work.
Instead say:
while read -r name; do
echo "$name"
FILENAME="$(dirname $name)"
done < <(grep -i -e "\.[a-zA-z]\{3\}$" ./test.txt)
The <(command) syntax is referred to as Process Substitution.
for i in $(RESULT) isn't right.You can use $RESULT or ${RESULT}
Related
I need to write a script that checks some >20k files for some >2k search text and it needs to be flexible, so I came up with this script:
#!/bin/bash
# This script checks all files in a given directory against a list of criteria
shopt -s expand_aliases
source ~/.bashrc
TIMESTAMP=$(date "+%Y-%m-%d-%T")
ROOT_DIR=/data
PROJECT_NAME=$1
FILE_DIR=$ROOT_DIR/projects/$1/$2
RESULT_DIR=$ROOT_DIR/projects/$1/check_result
SEARCHTEXT_FILE=$ROOT_DIR/scripts/$3
OIFS="$IFS"
IFS=$'\n'
files=$(find $FILE_DIR -type f -name '*.json')
for file in $files; do
while read line; do
grep -H -o $line "$file" >> $RESULT_DIR/check_result_$TIMESTAMP.log
done < $SEARCHTEXT_FILE
done
IFS="$OIFS"
This script only produces the empty $RESULT_DIR/check_result_$TIMESTAMP.log log file with correct name.
Because the file names sometimes contain spaces I added the IFS... statements and I enclosed $file in " quotes (copied from another post).
The content of the $SEARCHTEXT_FILE is for example:
'Tel alt........'
'City ..........'
If I place an echo before the grep like this
echo grep -H -o $line "$file"
then output I get is
grep -H -o 'Tel alt........' /data/projects/DNAR/input/report-157538.json
and I can execute this line as is and get the correct result.
I tried to put various combinations of " or ' or ` or () or {} around any part of this grep command but nothing changed.
Somewhere I did read about alias and the alias set for grep is
alias grep='grep --color=auto'
After many hours of searching on the internet I couldn't find any post that helped me as most of them are covering issues around wrong quotes or inline bash issues.
What are I missing here?
The simple and obvious workaround is to remove all that complexity and simply use the features of the commands you are running anyway.
find "$FILE_DIR" -type f -name '*.json' \
-exec grep -H -o -f "$SEARCHTEXT_FILE" {} + > "$RESULT_DIR/check_result_$TIMESTAMP.log"
Notice also the quoting fixes; see When to wrap quotes around a shell variable; to avoid mishaps, you should switch to lower case for your private variables (see Correct Bash and shell script variable capitalization).
shopt -s expand_aliases
and source ~/.bashrc merely look superfluous, but could contribute to whatever problem you are trying to troubleshoot; they should basically never be part of a script you plan to use in production.
I'm trying to find all the init scripts created for websphere.
I know all the scripts end up with -init, so the first part of the code is:
find /etc/rc.d/init.d -name "*-init"
Also, I need all the script that run on an specific path, so the second part would be
| grep -i "/opt/ibm"
Finally, I need help with the last part. I have found the scripts I need to run them with the stop argument.
find /etc/rc.d/init.d -name "*-init" | grep -i "/opt/ibm" | <<run script found with stop argument>>
How can I run the command found with find?
Use a loop so that we are a little more careful while executing them:
#!/bin/bash
shopt -s globstar
for file in /etc/rc.d/init.d/**/*-init; do # grab all -init scripts
script=$(readlink -f "$file") # grab the actual file in case of a symlink
[[ -f $script ]] || continue # skip if not a regular file
[[ $file = */opt/ibm/* ]] || continue # not "/opt/ibm/", skip
printf '%s\n' "Executing script '$script'"
"$script" stop; exit_code=$?
printf '%s\n' "Script '$script' finished with exit_code $exit_code"
done
If you omit the 'find' and use grep directly you could do something like this:
grep -i "/opt/ibm" /etc/rc.d/init.d/* | sed 's/:.*/ stop/g' | sort -u | bash
it uses grep directly, which adds the filename to the output: filename:matched line
since you only need the filename and not the match, use sed to replace the ':' and the rest of the line with ' stop' (see the space before stop)
use sort -u (make sure, to execute each script only once)
Pipe the result into a shell
echo -n "*.xcodeproj directory: ";
read fileDirectory;
echo -n $fileDirectory;
fileExtension="pbxproj";
find $fileDirectory -name "*.${fileExtension}";
It shows "find: XXXX"(fileDirectory) no such file or directory
However if I replace read fileDirectory by
fileDirectory=$1
It works.
So what's the difference?
$1 is the first argument passed to bash script or to a function inside the script
for example:
mybashfunction /dirtofind
inside the function if you write:
echo "$1"
It should print:
/dirtofind
Edit 1:
You must place the shebang in the beginning of you file
~$ cat a.sh
#!/bin/bash
echo -n "*.xcodeproj directory: ";
read fileDirectory;
echo -n $fileDirectory;
fileExtension="pbxproj";
find "$fileDirectory" -name "*.${fileExtension}";
~$ chmod +x a.sh
~$ ./a.sh
*.xcodeproj directory: /home
/home/home/leonardo/Qt/Tools/QtCreator/share/qtcreator/qbs/share/qbs/examples/cocoa-touch-application/CocoaTouchApplication.xcodeproj/project.pbxproj
/home/leonardo/Qt/Tools/QtCreator/share/qtcreator/qbs/share/qbs/examples/cocoa-application/CocoaApplication.xcodeproj/project.pbxproj
:~$
Works like charm here. Place the shebang
#!/bin/bash
Edit 2
Yes you can use eval. Your script will be like this:
#!/bin/bash
echo -n "*.xcodeproj directory: ";
read fileDirectory;
echo -n $fileDirectory;
fileExtension="pbxproj";
eval fileDirectory=$fileDirectory
find "$fileDirectory" -name "*.${fileExtension}";
read reads data from STDIN (by default), not from positional parameters (arguments).
As you are passing the data as first argument ($1) to the script, read would not catch it; it would catch the input you are providing interactively.
Just to note, you should quote your variable expansions to avoid word splitting and pathname expansion; these are unwanted in most cases.
While renaming multiple file in AIX using for loop I am getting error
${fn/$eisinno/$efilename}": 0403-011 The specified substitution is not valid for this command.
Input File:
raj_10576_INE728J01019_arya1.pdf
ram_10576_INE728J01019_arya1.pdf
rhaul_10576_INE728J01019_arya1.pdf
sanjay_10576_INE728J01019_arya1.pdf
dinesh_10576_INE728J01019_arya1.pdf
Desired Output File:
raj_10576_Remote_sag.pdf
ram_10576_Remote_sag.pdf
rhaul_10576_Remote_sag.pdf
sanjay_10576_Remote_sag.pdf
dinesh_10576_Remote_sag.pdf
My script is as follow:
#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"
cd /home/rishabh/$eveno
for file in *_$eveno_*.pdf
do
mv -i "${file}" "${file/$eveno_$eisinno/$eveno_remote_$efilename}"
done
Kindly help me
Use double n in the evennos and use braces to make sure where a variable ends:
#!/bin/bash
eisinno="INE728J01019_arya1.pdf"
evenno=10576
efilename="remote_sag.pdf"
cd /home/rishabh/${evenno}
for file in *_${evenno}_*.pdf; do
echo "Debug: ${file} ==> ${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
# Alternative:
echo ${file} | sed "s/${evenno}_${eisinno}/${evenno}_remote_${efilename}/"
mv -i "${file}" "${file/${evenno}_${eisinno}/${evenno}_remote_${efilename}}"
done
I have this simple bash script:
I run ns simulator on each file passed in argument where last argument is some text string to search for.
#!/bin/bash
nsloc="/home/ashish/ns-allinone-2.35/ns-2.35/ns"
temp="temp12345ashish.temp"
j=1
for file in "$#"
do
if [ $j -lt $# ]
then
let j=$j+1
`$nsloc $file > $temp 2>&1`
if grep -l ${BASH_ARGV[0]} $temp
then
echo "$file Successful"
fi
fi
done
I expected:
file1.tcl Successful
I am getting:
temp12345ashish.temp
file1.tcl Successful
When i run the simulator command myself on the terminal i do not get the file name to which output is directed.
I am not getting from where this first line of output is getting printed.
Please explain it.
Thanks in advance.
See man grep, and see specifically the explanation of the -l option.
In your script (above), you are using -l, so grep is telling you (as instructed) the filename where the match occurred.
If you don't want to see the filename, don't use -l, or use -q with it also. Eg:
grep -ql ${BASH_ARGV[0]} $temp
Just silence the grep:
if grep -l ${BASH_ARGV[0]} $temp &> /dev/null