How to debug the argument problem in Linux at the bash [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The question is contents of the variable $TEST are equal to the string “success, or the
number 5 or the contents of the variable $RESULT
The code that I write is [ $TEST=”success” -o $TEST -eq ”5” -o $TEST = “$RESULT” ], but when in the linux there saying that bash: [ too many argument. Can someone help me

You will need || for this, to represent or and so:
if [[ "$TEST"==”success” || "$TEST"==”5” || "$TEST" == “$RESULT” ]]
then
echo "match"
else
echo "No match"
fi

Related

File exists in shell script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to check file exists in a folder. I have a test.sh and test.json files in a folder. In the test.sh file I have scripts for checking whether the test.json file exists. The below code i have used for checking the existance. I have used the ls command and it shows the file exists but the code I have used is not finding the file and it prints file not exists.
can you pls help me whether I have missed anything here.
ls
File=test.json
echo "$FILE"
if [ -f "$FILE" ]; then
echo "file exists"
else
echo "file not exists"
fi
bash is case sensitive, you need to use same case in declaration and usage.
e.g.
FILE=test.json
echo "$FILE"
if [ -f "$FILE" ]; then
...

how to use grep command in .log file if it is not empty [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i have a log file. When the log file is not empty then i should grep command for policy and work flow. i have tried this code, but how can I initialize the log file and use this command?
cmd=`grep -c "POLICY" file`
if [[ $(grep -c "POLICY" file) -gt 0 ]]
then
echo "POLICY are present"
else
echo "POLICY not present"
fi
You can use grep -q. It will return 0 (success) if any match is found, otherwise (even if the files does not exist) it will return 1 (failure).
if grep -q POLICY file; then
echo "POLICY are present"
else
echo "POLICY not present"
fi

Using single quotes still giving "event not found" in bash [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am using single quotes but shell is interpreting it as a history expansion.
#!/bin/bash
if [ -z `$1` ] || [ -z `$2` ]
then
echo `Wrong ID` >&2
exit 1
fi
You should use double quotes " " instead of backticks ` `.
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]
then
echo "Wrong ID" >&2
exit 1
fi

Too many arguments on ubuntu server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am getting the error "line 22 : [: too many arguments" on ubuntu server, and I not too sure how to fix it. Just wondering is there any solution for it? Here's my code.
if [ $? -eq 0 ]; then
if [ $name = $ufname ]; then
echo "Names are still the same"
fi
fi
One must be very careful with variables that may contain spaces or other special characters. Whitespace can really wrench up the works.
The best solution in bash is to use [[ instead of [. It handles whitespace with grace and style. I recommend just switching to [[ in all cases and never using [. [[ is better in all respects.
if [[ $? -eq 0 ]]; then
if [[ $name = "$ufname" ]]; then
echo "Names are still the same"
fi
fi
The only reason to use [ is if portability is a concern--like if you're writing for plain sh rather than bash. In that case, you'll have to stick with [, and so you should quote your variables.
if [ $? -eq 0 ]; then
if [ "$name" = "$ufname" ]; then
echo "Names are still the same"
fi
fi

How do you list all symlinks in a directory that has non-hanging links? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I would like to get a list of all symlinks within a directory that has valid links. In other words, I would like all the broken links to be discarded in my list.
In shell, [ -L "$f" ] && [ -e "$f" ] is true if and only if "$f" is the name of a symlink whose target exists. So:
for f in *; do
if [ -L "$f" ] && [ -e "$f" ]; then
# do something with "$f"
fi
done
(Never use the -a or -o options to test/[...]; they cannot be relied on to have sane precedence.)

Resources