moving script from unix to linux bash
I have a script that I'm moving from a Solaris 11.3 environment to Oracle Linux 6.9 (used for exporting databases). After updating various variables and paths the script runs correctly except for one section.
What it's supposed to do is check the target directory for other exports and delete those in excess of the indicated keep setting (in this case it's 1). When the 'cleanup' section of the script runs it's properly identifying the files, but not removing them, indicating 'binary operator expected'.
The following is the section of the script that isn't working correctly:
FILE_MASK="$ORACLE_SID_*.dmp.gz"
counter=0
if [[ -f $EXP_DIR/$FILE_MASK ]]; then
#
# count the files (don't use ls command because it has a limit)
#
for CNT in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
done
count=`expr $counter - $KEEP_FILES + 1`
#
counter=0
for I in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
FILE_NAME=`basename $I`
echo "$JOBNAME_SHORT: File = $EXP_DIR/$FILE_NAME"
if [[ $counter -lt $count ]] ; then
echo "$JOBNAME_SHORT: Removing file."
rm $EXP_DIR/$FILE_NAME
fi
done
The error returned is:
/home/oracle/scripts/oraexpdp.sh: line 241: [: /u07/exports/xxxxxxxx/xxxxxxxx_2018111413.dmp.gz: binary operator expected
Line 241 of the script is the line starting with 'if' above.
In searching for a fix there was a reference to using double-bracketing ([[ ]]) with bash, and that still returns the same error. What do I need to tweak to get this working correctly?
Many Thanks!
Harvey
bash
add a comment |
I have a script that I'm moving from a Solaris 11.3 environment to Oracle Linux 6.9 (used for exporting databases). After updating various variables and paths the script runs correctly except for one section.
What it's supposed to do is check the target directory for other exports and delete those in excess of the indicated keep setting (in this case it's 1). When the 'cleanup' section of the script runs it's properly identifying the files, but not removing them, indicating 'binary operator expected'.
The following is the section of the script that isn't working correctly:
FILE_MASK="$ORACLE_SID_*.dmp.gz"
counter=0
if [[ -f $EXP_DIR/$FILE_MASK ]]; then
#
# count the files (don't use ls command because it has a limit)
#
for CNT in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
done
count=`expr $counter - $KEEP_FILES + 1`
#
counter=0
for I in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
FILE_NAME=`basename $I`
echo "$JOBNAME_SHORT: File = $EXP_DIR/$FILE_NAME"
if [[ $counter -lt $count ]] ; then
echo "$JOBNAME_SHORT: Removing file."
rm $EXP_DIR/$FILE_NAME
fi
done
The error returned is:
/home/oracle/scripts/oraexpdp.sh: line 241: [: /u07/exports/xxxxxxxx/xxxxxxxx_2018111413.dmp.gz: binary operator expected
Line 241 of the script is the line starting with 'if' above.
In searching for a fix there was a reference to using double-bracketing ([[ ]]) with bash, and that still returns the same error. What do I need to tweak to get this working correctly?
Many Thanks!
Harvey
bash
What is the first line of the script? Is it#!/bin/sh
or#!/bin/bash
(or something else)?
– legoscia
Nov 15 '18 at 16:58
From what I can tell, when you're settingFILE_MASK="$ORACLE_SID_*.dmp.gz"
you're using a*
wildcard? This is taken as a literal, not a wildcard in "double quotes" you'd reference it differently but regex would be.*
I can't actually see if you closed yourif
loop withfi
, so maybe just list the relevant bit of the script
– itChi
Nov 15 '18 at 17:15
The first line is !bin/sh
– HarveyB
Nov 15 '18 at 17:38
The script has three similar sections looking for different specific files. The closing fi is actually after the third section. There is an else followed by an fi that would be after the done statement above.
– HarveyB
Nov 15 '18 at 17:41
add a comment |
I have a script that I'm moving from a Solaris 11.3 environment to Oracle Linux 6.9 (used for exporting databases). After updating various variables and paths the script runs correctly except for one section.
What it's supposed to do is check the target directory for other exports and delete those in excess of the indicated keep setting (in this case it's 1). When the 'cleanup' section of the script runs it's properly identifying the files, but not removing them, indicating 'binary operator expected'.
The following is the section of the script that isn't working correctly:
FILE_MASK="$ORACLE_SID_*.dmp.gz"
counter=0
if [[ -f $EXP_DIR/$FILE_MASK ]]; then
#
# count the files (don't use ls command because it has a limit)
#
for CNT in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
done
count=`expr $counter - $KEEP_FILES + 1`
#
counter=0
for I in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
FILE_NAME=`basename $I`
echo "$JOBNAME_SHORT: File = $EXP_DIR/$FILE_NAME"
if [[ $counter -lt $count ]] ; then
echo "$JOBNAME_SHORT: Removing file."
rm $EXP_DIR/$FILE_NAME
fi
done
The error returned is:
/home/oracle/scripts/oraexpdp.sh: line 241: [: /u07/exports/xxxxxxxx/xxxxxxxx_2018111413.dmp.gz: binary operator expected
Line 241 of the script is the line starting with 'if' above.
In searching for a fix there was a reference to using double-bracketing ([[ ]]) with bash, and that still returns the same error. What do I need to tweak to get this working correctly?
Many Thanks!
Harvey
bash
I have a script that I'm moving from a Solaris 11.3 environment to Oracle Linux 6.9 (used for exporting databases). After updating various variables and paths the script runs correctly except for one section.
What it's supposed to do is check the target directory for other exports and delete those in excess of the indicated keep setting (in this case it's 1). When the 'cleanup' section of the script runs it's properly identifying the files, but not removing them, indicating 'binary operator expected'.
The following is the section of the script that isn't working correctly:
FILE_MASK="$ORACLE_SID_*.dmp.gz"
counter=0
if [[ -f $EXP_DIR/$FILE_MASK ]]; then
#
# count the files (don't use ls command because it has a limit)
#
for CNT in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
done
count=`expr $counter - $KEEP_FILES + 1`
#
counter=0
for I in $EXP_DIR/$FILE_MASK
do
counter=`expr $counter + 1`
FILE_NAME=`basename $I`
echo "$JOBNAME_SHORT: File = $EXP_DIR/$FILE_NAME"
if [[ $counter -lt $count ]] ; then
echo "$JOBNAME_SHORT: Removing file."
rm $EXP_DIR/$FILE_NAME
fi
done
The error returned is:
/home/oracle/scripts/oraexpdp.sh: line 241: [: /u07/exports/xxxxxxxx/xxxxxxxx_2018111413.dmp.gz: binary operator expected
Line 241 of the script is the line starting with 'if' above.
In searching for a fix there was a reference to using double-bracketing ([[ ]]) with bash, and that still returns the same error. What do I need to tweak to get this working correctly?
Many Thanks!
Harvey
bash
bash
asked Nov 15 '18 at 16:42
HarveyBHarveyB
1
1
What is the first line of the script? Is it#!/bin/sh
or#!/bin/bash
(or something else)?
– legoscia
Nov 15 '18 at 16:58
From what I can tell, when you're settingFILE_MASK="$ORACLE_SID_*.dmp.gz"
you're using a*
wildcard? This is taken as a literal, not a wildcard in "double quotes" you'd reference it differently but regex would be.*
I can't actually see if you closed yourif
loop withfi
, so maybe just list the relevant bit of the script
– itChi
Nov 15 '18 at 17:15
The first line is !bin/sh
– HarveyB
Nov 15 '18 at 17:38
The script has three similar sections looking for different specific files. The closing fi is actually after the third section. There is an else followed by an fi that would be after the done statement above.
– HarveyB
Nov 15 '18 at 17:41
add a comment |
What is the first line of the script? Is it#!/bin/sh
or#!/bin/bash
(or something else)?
– legoscia
Nov 15 '18 at 16:58
From what I can tell, when you're settingFILE_MASK="$ORACLE_SID_*.dmp.gz"
you're using a*
wildcard? This is taken as a literal, not a wildcard in "double quotes" you'd reference it differently but regex would be.*
I can't actually see if you closed yourif
loop withfi
, so maybe just list the relevant bit of the script
– itChi
Nov 15 '18 at 17:15
The first line is !bin/sh
– HarveyB
Nov 15 '18 at 17:38
The script has three similar sections looking for different specific files. The closing fi is actually after the third section. There is an else followed by an fi that would be after the done statement above.
– HarveyB
Nov 15 '18 at 17:41
What is the first line of the script? Is it
#!/bin/sh
or #!/bin/bash
(or something else)?– legoscia
Nov 15 '18 at 16:58
What is the first line of the script? Is it
#!/bin/sh
or #!/bin/bash
(or something else)?– legoscia
Nov 15 '18 at 16:58
From what I can tell, when you're setting
FILE_MASK="$ORACLE_SID_*.dmp.gz"
you're using a *
wildcard? This is taken as a literal, not a wildcard in "double quotes" you'd reference it differently but regex would be .*
I can't actually see if you closed your if
loop with fi
, so maybe just list the relevant bit of the script– itChi
Nov 15 '18 at 17:15
From what I can tell, when you're setting
FILE_MASK="$ORACLE_SID_*.dmp.gz"
you're using a *
wildcard? This is taken as a literal, not a wildcard in "double quotes" you'd reference it differently but regex would be .*
I can't actually see if you closed your if
loop with fi
, so maybe just list the relevant bit of the script– itChi
Nov 15 '18 at 17:15
The first line is !bin/sh
– HarveyB
Nov 15 '18 at 17:38
The first line is !bin/sh
– HarveyB
Nov 15 '18 at 17:38
The script has three similar sections looking for different specific files. The closing fi is actually after the third section. There is an else followed by an fi that would be after the done statement above.
– HarveyB
Nov 15 '18 at 17:41
The script has three similar sections looking for different specific files. The closing fi is actually after the third section. There is an else followed by an fi that would be after the done statement above.
– HarveyB
Nov 15 '18 at 17:41
add a comment |
1 Answer
1
active
oldest
votes
[[ -f $EXP_DIR/$FILE_MASK ]]
is going to give a group of files because of wild card bash substitution, so wont evaluate that a file exists.
In the case where there are no matching files $EXP_DIR/$FILE_MASK
will be wildcard string still.
Assuming that KEEP_FILES
will always be 1 or more, you could probably just remove the if [[ -f $EXP_DIR/$FILE_MASK ]]
as in the case of no files, it wouldn't delete anything, as there would be just the wildcard name,
in the case of multiple files, it will behave as normal.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f53324102%2fmoving-script-from-unix-to-linux-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
[[ -f $EXP_DIR/$FILE_MASK ]]
is going to give a group of files because of wild card bash substitution, so wont evaluate that a file exists.
In the case where there are no matching files $EXP_DIR/$FILE_MASK
will be wildcard string still.
Assuming that KEEP_FILES
will always be 1 or more, you could probably just remove the if [[ -f $EXP_DIR/$FILE_MASK ]]
as in the case of no files, it wouldn't delete anything, as there would be just the wildcard name,
in the case of multiple files, it will behave as normal.
add a comment |
[[ -f $EXP_DIR/$FILE_MASK ]]
is going to give a group of files because of wild card bash substitution, so wont evaluate that a file exists.
In the case where there are no matching files $EXP_DIR/$FILE_MASK
will be wildcard string still.
Assuming that KEEP_FILES
will always be 1 or more, you could probably just remove the if [[ -f $EXP_DIR/$FILE_MASK ]]
as in the case of no files, it wouldn't delete anything, as there would be just the wildcard name,
in the case of multiple files, it will behave as normal.
add a comment |
[[ -f $EXP_DIR/$FILE_MASK ]]
is going to give a group of files because of wild card bash substitution, so wont evaluate that a file exists.
In the case where there are no matching files $EXP_DIR/$FILE_MASK
will be wildcard string still.
Assuming that KEEP_FILES
will always be 1 or more, you could probably just remove the if [[ -f $EXP_DIR/$FILE_MASK ]]
as in the case of no files, it wouldn't delete anything, as there would be just the wildcard name,
in the case of multiple files, it will behave as normal.
[[ -f $EXP_DIR/$FILE_MASK ]]
is going to give a group of files because of wild card bash substitution, so wont evaluate that a file exists.
In the case where there are no matching files $EXP_DIR/$FILE_MASK
will be wildcard string still.
Assuming that KEEP_FILES
will always be 1 or more, you could probably just remove the if [[ -f $EXP_DIR/$FILE_MASK ]]
as in the case of no files, it wouldn't delete anything, as there would be just the wildcard name,
in the case of multiple files, it will behave as normal.
answered Nov 15 '18 at 17:13
lostbardlostbard
3,1431412
3,1431412
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53324102%2fmoving-script-from-unix-to-linux-bash%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
What is the first line of the script? Is it
#!/bin/sh
or#!/bin/bash
(or something else)?– legoscia
Nov 15 '18 at 16:58
From what I can tell, when you're setting
FILE_MASK="$ORACLE_SID_*.dmp.gz"
you're using a*
wildcard? This is taken as a literal, not a wildcard in "double quotes" you'd reference it differently but regex would be.*
I can't actually see if you closed yourif
loop withfi
, so maybe just list the relevant bit of the script– itChi
Nov 15 '18 at 17:15
The first line is !bin/sh
– HarveyB
Nov 15 '18 at 17:38
The script has three similar sections looking for different specific files. The closing fi is actually after the third section. There is an else followed by an fi that would be after the done statement above.
– HarveyB
Nov 15 '18 at 17:41