Shell Script - Check if a file contains a specific line/stringShell script check if file exists?String...
Trivial cases of shared_ptr and weak_ptr failing
Why does "git status" show I'm on the master branch and "git branch" does not?
Intel 8080-based home computers
How can I leave a car for someone if we can't meet in person?
A necessary and sufficient condition for (x1,...,xn) to be a permutation of (1,...,n)
Do dragons smell of lilacs?
Edge-lit LED panel best materials?
Can you perfectly wrap a cube with this blocky shape?
Is it ethical for a company to ask its employees to move furniture on a weekend?
Why should I cook the flour first when making bechamel sauce?
Can a Resident Assistant Be Told to Ignore a Lawful Order?
Is it rude to refer to janitors as 'floor people'?
(Piano) is the purpose of sheet music to be played along to? Or a guide for learning and reference during playing?
What happens if there is no space for entry stamp in the passport for US visa?
Do aircraft cabins have suspension?
A scene of Jimmy diversity
Can a dragon's breath weapon pass through Leomund's Tiny Hut?
Can a pizza stone be fixed after soap has been used to clean it?
Is the Malay "garam" (salt) related to the Latin "garum" (fish sauce)?
How fast does a character need to move to be effectively invisible?
What are the arguments for California’s nonpartisan blanket (jungle) primaries?
Advice for paying off student loans and auto loans now that I have my first 'real' job
How to determine the optimal threshold to achieve the highest accuracy
What problems was on a lunar module of Apollo 11?
Shell Script - Check if a file contains a specific line/string
Shell script check if file exists?String Manipulation Shell ScriptHow can I delete a line from a file when the line contains a specific string?Print check/cross mark in shell scriptString length with shell scriptShell: Check line format file and loop over file linesShell script: split lineWant to cd within a bash script and then run “git checkout”How to get du -ksh working without a carriage return in shell-scripting?Bash String manipulation when string contains parentheses
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
my script contain :
#### #!/usr/bin/sh
FILE="/home/steven/.bash_profile"
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
if [ -z $(grep "$STRING" "$FILE") ]; then
echo 'the string is exist' >&2
else
echo 'the string doesnt exist' >&2
fi
and my file contain :
#### # .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#### # User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
why it's always return the is exist ?
please help for this one.
many thanks.
shell-script centos
add a comment |
my script contain :
#### #!/usr/bin/sh
FILE="/home/steven/.bash_profile"
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
if [ -z $(grep "$STRING" "$FILE") ]; then
echo 'the string is exist' >&2
else
echo 'the string doesnt exist' >&2
fi
and my file contain :
#### # .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#### # User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
why it's always return the is exist ?
please help for this one.
many thanks.
shell-script centos
-z
checks if the string is empty. Did you mean to use-n
? Or just usegrep
directly:if grep -q "$STRING" "$FILE"; then ...
– muru
48 mins ago
@muru i try grep directly but failed.
– Steven Audy
39 mins ago
add a comment |
my script contain :
#### #!/usr/bin/sh
FILE="/home/steven/.bash_profile"
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
if [ -z $(grep "$STRING" "$FILE") ]; then
echo 'the string is exist' >&2
else
echo 'the string doesnt exist' >&2
fi
and my file contain :
#### # .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#### # User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
why it's always return the is exist ?
please help for this one.
many thanks.
shell-script centos
my script contain :
#### #!/usr/bin/sh
FILE="/home/steven/.bash_profile"
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
if [ -z $(grep "$STRING" "$FILE") ]; then
echo 'the string is exist' >&2
else
echo 'the string doesnt exist' >&2
fi
and my file contain :
#### # .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
#### # User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
why it's always return the is exist ?
please help for this one.
many thanks.
shell-script centos
shell-script centos
edited 38 mins ago
Steven Audy
asked 54 mins ago
Steven AudySteven Audy
12 bronze badges
12 bronze badges
-z
checks if the string is empty. Did you mean to use-n
? Or just usegrep
directly:if grep -q "$STRING" "$FILE"; then ...
– muru
48 mins ago
@muru i try grep directly but failed.
– Steven Audy
39 mins ago
add a comment |
-z
checks if the string is empty. Did you mean to use-n
? Or just usegrep
directly:if grep -q "$STRING" "$FILE"; then ...
– muru
48 mins ago
@muru i try grep directly but failed.
– Steven Audy
39 mins ago
-z
checks if the string is empty. Did you mean to use -n
? Or just use grep
directly: if grep -q "$STRING" "$FILE"; then ...
– muru
48 mins ago
-z
checks if the string is empty. Did you mean to use -n
? Or just use grep
directly: if grep -q "$STRING" "$FILE"; then ...
– muru
48 mins ago
@muru i try grep directly but failed.
– Steven Audy
39 mins ago
@muru i try grep directly but failed.
– Steven Audy
39 mins ago
add a comment |
1 Answer
1
active
oldest
votes
We should escape the $
and "
in the STRING variable. otherwise, it will expand the $PATH
Declare it as:
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
As commented by @muru, try as
if grep -q "$STRING" "$FILE" ; then
echo 'the string is exist' ;
else
echo 'the string doesnt exist' ;
fi
Since export
is missing in line no 12 of file bashrc. it will always result as doesnt exist.
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
prefixexport
is missing in your bashrc
– msp9011
24 mins ago
removeexport
in the STRING variable. if it is not desired.
– msp9011
20 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f530561%2fshell-script-check-if-a-file-contains-a-specific-line-string%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
We should escape the $
and "
in the STRING variable. otherwise, it will expand the $PATH
Declare it as:
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
As commented by @muru, try as
if grep -q "$STRING" "$FILE" ; then
echo 'the string is exist' ;
else
echo 'the string doesnt exist' ;
fi
Since export
is missing in line no 12 of file bashrc. it will always result as doesnt exist.
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
prefixexport
is missing in your bashrc
– msp9011
24 mins ago
removeexport
in the STRING variable. if it is not desired.
– msp9011
20 mins ago
add a comment |
We should escape the $
and "
in the STRING variable. otherwise, it will expand the $PATH
Declare it as:
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
As commented by @muru, try as
if grep -q "$STRING" "$FILE" ; then
echo 'the string is exist' ;
else
echo 'the string doesnt exist' ;
fi
Since export
is missing in line no 12 of file bashrc. it will always result as doesnt exist.
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
prefixexport
is missing in your bashrc
– msp9011
24 mins ago
removeexport
in the STRING variable. if it is not desired.
– msp9011
20 mins ago
add a comment |
We should escape the $
and "
in the STRING variable. otherwise, it will expand the $PATH
Declare it as:
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
As commented by @muru, try as
if grep -q "$STRING" "$FILE" ; then
echo 'the string is exist' ;
else
echo 'the string doesnt exist' ;
fi
Since export
is missing in line no 12 of file bashrc. it will always result as doesnt exist.
We should escape the $
and "
in the STRING variable. otherwise, it will expand the $PATH
Declare it as:
STRING="export PATH="$PATH:/opt/mssql-tools/bin""
As commented by @muru, try as
if grep -q "$STRING" "$FILE" ; then
echo 'the string is exist' ;
else
echo 'the string doesnt exist' ;
fi
Since export
is missing in line no 12 of file bashrc. it will always result as doesnt exist.
edited 22 mins ago
answered 44 mins ago
msp9011msp9011
5,3095 gold badges42 silver badges69 bronze badges
5,3095 gold badges42 silver badges69 bronze badges
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
prefixexport
is missing in your bashrc
– msp9011
24 mins ago
removeexport
in the STRING variable. if it is not desired.
– msp9011
20 mins ago
add a comment |
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
prefixexport
is missing in your bashrc
– msp9011
24 mins ago
removeexport
in the STRING variable. if it is not desired.
– msp9011
20 mins ago
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
#!/usr/bin/sh FILE="/home/steven/.bash_profile" STRING="export PATH="$PATH:/opt/mssql-tools/bin"" if [ -z $(grep "$STRING" "$FILE") ]; then echo 'the string is exist' >&2 else echo 'the string doesnt exist' >&2 fi it return value : 'the string is exist' again in my file there's no export PATH="$PATH:/opt/mssql-tools/bin"
– Steven Audy
33 mins ago
prefix
export
is missing in your bashrc– msp9011
24 mins ago
prefix
export
is missing in your bashrc– msp9011
24 mins ago
remove
export
in the STRING variable. if it is not desired.– msp9011
20 mins ago
remove
export
in the STRING variable. if it is not desired.– msp9011
20 mins ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f530561%2fshell-script-check-if-a-file-contains-a-specific-line-string%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
-z
checks if the string is empty. Did you mean to use-n
? Or just usegrep
directly:if grep -q "$STRING" "$FILE"; then ...
– muru
48 mins ago
@muru i try grep directly but failed.
– Steven Audy
39 mins ago