search for a string in a file does not work as expected inside a loop in shell script
up vote
-1
down vote
favorite
I have zip files in multiple directories. I need to go to each directory and extract the sip files and search for a string inside one of the file in extracted folder.
My Script as follows:
#!/bin/bash
Path="/xyz/test_sand/"
cd $Path
for pro in $Path*;
do
[ -d $pro ] && cd "$pro"
echo $pro
#To identify the correct zip file
xml=$pro'/versions.xml'
echo $xml
declare -a vnum
mapfile -t vnum < <(xml_grep 'number' $xml --text_only)
vn=${#vnum[@]}
echo $vn ;
num=1
declare -i res
res=$vn-$num
mkdir "/home/Ras/zfol"
unzip $pro'/model'$res'.xml.zip' -d "/home/Ras/zfol"
prpath="/home/Ras/zfol/com.nomagic.ci.metamodel.project"
if grep -q 'Struts_profile' $prpath
then
echo "struts exists in project $pro" >> "/home/Ras/struts.txt"
else
echo "no struts in project"
fi
cd "/home/Ras/"
rm -rf "zfol"
done;
When I hardcode the directory that is without for loop, grep gives proper result. In side for loop, even when my file contains the string"Struts_profile", it goes to else condition.
Please help me to resolve this and identify what is the problem in my script.
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). This number is available in the variable 'res' in my script. Using unzip the file is getting extracted too. Extracted files are available in /home/Ras/zfol . In extracted files i have a file named com.nomagic.ci.metamodel.project in which i have to check whether there is a string "Struts_profile" or not. If the string is found then i have to get the respective directory (xyz or abc or def) name in a file struts.txt ("/home/Ras/struts.txt").
bash shell
add a comment |
up vote
-1
down vote
favorite
I have zip files in multiple directories. I need to go to each directory and extract the sip files and search for a string inside one of the file in extracted folder.
My Script as follows:
#!/bin/bash
Path="/xyz/test_sand/"
cd $Path
for pro in $Path*;
do
[ -d $pro ] && cd "$pro"
echo $pro
#To identify the correct zip file
xml=$pro'/versions.xml'
echo $xml
declare -a vnum
mapfile -t vnum < <(xml_grep 'number' $xml --text_only)
vn=${#vnum[@]}
echo $vn ;
num=1
declare -i res
res=$vn-$num
mkdir "/home/Ras/zfol"
unzip $pro'/model'$res'.xml.zip' -d "/home/Ras/zfol"
prpath="/home/Ras/zfol/com.nomagic.ci.metamodel.project"
if grep -q 'Struts_profile' $prpath
then
echo "struts exists in project $pro" >> "/home/Ras/struts.txt"
else
echo "no struts in project"
fi
cd "/home/Ras/"
rm -rf "zfol"
done;
When I hardcode the directory that is without for loop, grep gives proper result. In side for loop, even when my file contains the string"Struts_profile", it goes to else condition.
Please help me to resolve this and identify what is the problem in my script.
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). This number is available in the variable 'res' in my script. Using unzip the file is getting extracted too. Extracted files are available in /home/Ras/zfol . In extracted files i have a file named com.nomagic.ci.metamodel.project in which i have to check whether there is a string "Struts_profile" or not. If the string is found then i have to get the respective directory (xyz or abc or def) name in a file struts.txt ("/home/Ras/struts.txt").
bash shell
5
This would be a lot easier to answer if your code was cleaner and easier to read. First, run the code through shellcheck.net and fix the problems it points out. If that doesn't fix it, please provide a minimal, complete, and verifiable example, which basically means removing irrelevant parts of the script (and if taking a piece out removes the problem, then it's not irrelevant so put it back) until all that's left is the relevant parts. Also, indent the code properly -- this helps readability a great deal!
– Gordon Davisson
Nov 10 at 21:16
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). To get that number i have following piece of code in my script:declare -a vnum mapfile -t vnum < <(xml_grep 'number' $xml --text_only) vn=${#vnum[@]} echo $vn ; num=1 declare -i res res=$vn-$num
– Ras
Nov 11 at 6:00
I have tried checking my script in Shellcheck,,but didnt find any problem. When i pass the respective directory name xyz after extracting the zip file grep command search for the string and it gives the output correctly. But same thing inside for gives result incorrectly. What may be the reason for this. Inside for also i could see that file extraction is happening ..still what may the problem here?
– Ras
Nov 11 at 6:12
1
shellcheck points out a number of problems in the script, mostly variable references without double-quotes around them andcdcommands without error checking; these may not be causing the problem you're seeing, but they really should be fixed and there's no point looking in more detail until they're solved. Also, as I said, your script is very hard to read; please don't ask someone to put the effort into deciphering it if you aren't willing to put the some effort into cleaning it up first.
– Gordon Davisson
Nov 11 at 6:55
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have zip files in multiple directories. I need to go to each directory and extract the sip files and search for a string inside one of the file in extracted folder.
My Script as follows:
#!/bin/bash
Path="/xyz/test_sand/"
cd $Path
for pro in $Path*;
do
[ -d $pro ] && cd "$pro"
echo $pro
#To identify the correct zip file
xml=$pro'/versions.xml'
echo $xml
declare -a vnum
mapfile -t vnum < <(xml_grep 'number' $xml --text_only)
vn=${#vnum[@]}
echo $vn ;
num=1
declare -i res
res=$vn-$num
mkdir "/home/Ras/zfol"
unzip $pro'/model'$res'.xml.zip' -d "/home/Ras/zfol"
prpath="/home/Ras/zfol/com.nomagic.ci.metamodel.project"
if grep -q 'Struts_profile' $prpath
then
echo "struts exists in project $pro" >> "/home/Ras/struts.txt"
else
echo "no struts in project"
fi
cd "/home/Ras/"
rm -rf "zfol"
done;
When I hardcode the directory that is without for loop, grep gives proper result. In side for loop, even when my file contains the string"Struts_profile", it goes to else condition.
Please help me to resolve this and identify what is the problem in my script.
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). This number is available in the variable 'res' in my script. Using unzip the file is getting extracted too. Extracted files are available in /home/Ras/zfol . In extracted files i have a file named com.nomagic.ci.metamodel.project in which i have to check whether there is a string "Struts_profile" or not. If the string is found then i have to get the respective directory (xyz or abc or def) name in a file struts.txt ("/home/Ras/struts.txt").
bash shell
I have zip files in multiple directories. I need to go to each directory and extract the sip files and search for a string inside one of the file in extracted folder.
My Script as follows:
#!/bin/bash
Path="/xyz/test_sand/"
cd $Path
for pro in $Path*;
do
[ -d $pro ] && cd "$pro"
echo $pro
#To identify the correct zip file
xml=$pro'/versions.xml'
echo $xml
declare -a vnum
mapfile -t vnum < <(xml_grep 'number' $xml --text_only)
vn=${#vnum[@]}
echo $vn ;
num=1
declare -i res
res=$vn-$num
mkdir "/home/Ras/zfol"
unzip $pro'/model'$res'.xml.zip' -d "/home/Ras/zfol"
prpath="/home/Ras/zfol/com.nomagic.ci.metamodel.project"
if grep -q 'Struts_profile' $prpath
then
echo "struts exists in project $pro" >> "/home/Ras/struts.txt"
else
echo "no struts in project"
fi
cd "/home/Ras/"
rm -rf "zfol"
done;
When I hardcode the directory that is without for loop, grep gives proper result. In side for loop, even when my file contains the string"Struts_profile", it goes to else condition.
Please help me to resolve this and identify what is the problem in my script.
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). This number is available in the variable 'res' in my script. Using unzip the file is getting extracted too. Extracted files are available in /home/Ras/zfol . In extracted files i have a file named com.nomagic.ci.metamodel.project in which i have to check whether there is a string "Struts_profile" or not. If the string is found then i have to get the respective directory (xyz or abc or def) name in a file struts.txt ("/home/Ras/struts.txt").
bash shell
bash shell
edited Nov 11 at 6:09
asked Nov 10 at 20:00
Ras
265
265
5
This would be a lot easier to answer if your code was cleaner and easier to read. First, run the code through shellcheck.net and fix the problems it points out. If that doesn't fix it, please provide a minimal, complete, and verifiable example, which basically means removing irrelevant parts of the script (and if taking a piece out removes the problem, then it's not irrelevant so put it back) until all that's left is the relevant parts. Also, indent the code properly -- this helps readability a great deal!
– Gordon Davisson
Nov 10 at 21:16
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). To get that number i have following piece of code in my script:declare -a vnum mapfile -t vnum < <(xml_grep 'number' $xml --text_only) vn=${#vnum[@]} echo $vn ; num=1 declare -i res res=$vn-$num
– Ras
Nov 11 at 6:00
I have tried checking my script in Shellcheck,,but didnt find any problem. When i pass the respective directory name xyz after extracting the zip file grep command search for the string and it gives the output correctly. But same thing inside for gives result incorrectly. What may be the reason for this. Inside for also i could see that file extraction is happening ..still what may the problem here?
– Ras
Nov 11 at 6:12
1
shellcheck points out a number of problems in the script, mostly variable references without double-quotes around them andcdcommands without error checking; these may not be causing the problem you're seeing, but they really should be fixed and there's no point looking in more detail until they're solved. Also, as I said, your script is very hard to read; please don't ask someone to put the effort into deciphering it if you aren't willing to put the some effort into cleaning it up first.
– Gordon Davisson
Nov 11 at 6:55
add a comment |
5
This would be a lot easier to answer if your code was cleaner and easier to read. First, run the code through shellcheck.net and fix the problems it points out. If that doesn't fix it, please provide a minimal, complete, and verifiable example, which basically means removing irrelevant parts of the script (and if taking a piece out removes the problem, then it's not irrelevant so put it back) until all that's left is the relevant parts. Also, indent the code properly -- this helps readability a great deal!
– Gordon Davisson
Nov 10 at 21:16
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). To get that number i have following piece of code in my script:declare -a vnum mapfile -t vnum < <(xml_grep 'number' $xml --text_only) vn=${#vnum[@]} echo $vn ; num=1 declare -i res res=$vn-$num
– Ras
Nov 11 at 6:00
I have tried checking my script in Shellcheck,,but didnt find any problem. When i pass the respective directory name xyz after extracting the zip file grep command search for the string and it gives the output correctly. But same thing inside for gives result incorrectly. What may be the reason for this. Inside for also i could see that file extraction is happening ..still what may the problem here?
– Ras
Nov 11 at 6:12
1
shellcheck points out a number of problems in the script, mostly variable references without double-quotes around them andcdcommands without error checking; these may not be causing the problem you're seeing, but they really should be fixed and there's no point looking in more detail until they're solved. Also, as I said, your script is very hard to read; please don't ask someone to put the effort into deciphering it if you aren't willing to put the some effort into cleaning it up first.
– Gordon Davisson
Nov 11 at 6:55
5
5
This would be a lot easier to answer if your code was cleaner and easier to read. First, run the code through shellcheck.net and fix the problems it points out. If that doesn't fix it, please provide a minimal, complete, and verifiable example, which basically means removing irrelevant parts of the script (and if taking a piece out removes the problem, then it's not irrelevant so put it back) until all that's left is the relevant parts. Also, indent the code properly -- this helps readability a great deal!
– Gordon Davisson
Nov 10 at 21:16
This would be a lot easier to answer if your code was cleaner and easier to read. First, run the code through shellcheck.net and fix the problems it points out. If that doesn't fix it, please provide a minimal, complete, and verifiable example, which basically means removing irrelevant parts of the script (and if taking a piece out removes the problem, then it's not irrelevant so put it back) until all that's left is the relevant parts. Also, indent the code properly -- this helps readability a great deal!
– Gordon Davisson
Nov 10 at 21:16
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). To get that number i have following piece of code in my script:declare -a vnum mapfile -t vnum < <(xml_grep 'number' $xml --text_only) vn=${#vnum[@]} echo $vn ; num=1 declare -i res res=$vn-$num
– Ras
Nov 11 at 6:00
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). To get that number i have following piece of code in my script:declare -a vnum mapfile -t vnum < <(xml_grep 'number' $xml --text_only) vn=${#vnum[@]} echo $vn ; num=1 declare -i res res=$vn-$num
– Ras
Nov 11 at 6:00
I have tried checking my script in Shellcheck,,but didnt find any problem. When i pass the respective directory name xyz after extracting the zip file grep command search for the string and it gives the output correctly. But same thing inside for gives result incorrectly. What may be the reason for this. Inside for also i could see that file extraction is happening ..still what may the problem here?
– Ras
Nov 11 at 6:12
I have tried checking my script in Shellcheck,,but didnt find any problem. When i pass the respective directory name xyz after extracting the zip file grep command search for the string and it gives the output correctly. But same thing inside for gives result incorrectly. What may be the reason for this. Inside for also i could see that file extraction is happening ..still what may the problem here?
– Ras
Nov 11 at 6:12
1
1
shellcheck points out a number of problems in the script, mostly variable references without double-quotes around them and
cd commands without error checking; these may not be causing the problem you're seeing, but they really should be fixed and there's no point looking in more detail until they're solved. Also, as I said, your script is very hard to read; please don't ask someone to put the effort into deciphering it if you aren't willing to put the some effort into cleaning it up first.– Gordon Davisson
Nov 11 at 6:55
shellcheck points out a number of problems in the script, mostly variable references without double-quotes around them and
cd commands without error checking; these may not be causing the problem you're seeing, but they really should be fixed and there's no point looking in more detail until they're solved. Also, as I said, your script is very hard to read; please don't ask someone to put the effort into deciphering it if you aren't willing to put the some effort into cleaning it up first.– Gordon Davisson
Nov 11 at 6:55
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242904%2fsearch-for-a-string-in-a-file-does-not-work-as-expected-inside-a-loop-in-shell-s%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
This would be a lot easier to answer if your code was cleaner and easier to read. First, run the code through shellcheck.net and fix the problems it points out. If that doesn't fix it, please provide a minimal, complete, and verifiable example, which basically means removing irrelevant parts of the script (and if taking a piece out removes the problem, then it's not irrelevant so put it back) until all that's left is the relevant parts. Also, indent the code properly -- this helps readability a great deal!
– Gordon Davisson
Nov 10 at 21:16
I have my main directory /test_sand. Inside test_sand there are multiple directories like xyz, abc,def etc. Each of these directories contain many zip files like model1.xml.zip,model2.xml.zip etc.. I need to extract the latest zip file ie here model2.xml.zip (model number helps to identify which is the latest zip file..). To get that number i have following piece of code in my script:declare -a vnum mapfile -t vnum < <(xml_grep 'number' $xml --text_only) vn=${#vnum[@]} echo $vn ; num=1 declare -i res res=$vn-$num
– Ras
Nov 11 at 6:00
I have tried checking my script in Shellcheck,,but didnt find any problem. When i pass the respective directory name xyz after extracting the zip file grep command search for the string and it gives the output correctly. But same thing inside for gives result incorrectly. What may be the reason for this. Inside for also i could see that file extraction is happening ..still what may the problem here?
– Ras
Nov 11 at 6:12
1
shellcheck points out a number of problems in the script, mostly variable references without double-quotes around them and
cdcommands without error checking; these may not be causing the problem you're seeing, but they really should be fixed and there's no point looking in more detail until they're solved. Also, as I said, your script is very hard to read; please don't ask someone to put the effort into deciphering it if you aren't willing to put the some effort into cleaning it up first.– Gordon Davisson
Nov 11 at 6:55