Too many arguments on ubuntu server [closed] - linux

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

Related

How to debug the argument problem in Linux at the bash [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 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

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
...

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

I Can't get my bash code right to check an answer read in

My current script is like this but is for one answer.
A1="hp"
echo
echo 1.What os uses swinstall for software installs?
read answer
if [ "$answer == "$A1" ]; then
echo "correct"
else
echo "incorrect"
fi
(for questions with two answers)i would like to put in place of this see below i would like for them to be able to choose either answer up top though.
echo the answer could be hp or ux
What about using a case statement? This allows for a bit of flexibility:
A1="hp"
A2="ux"
echo
echo 1. What OS uses swinstall for software installs?
read answer
case $answer in
($A1) echo "correct"
;;
($A2) echo "correct"
;;
(*) echo "incorrect"
;;
esac

How to compare variables in for loop bash [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I want to create if statement, but in this statement are some errors
for i in ${#:2} ; do
if (( $2 -eq $i ))
then
continue
fi
done
How to fix my if statement
Your statement only works for integers.
If you want to compare them as strings, you can use [[ "string1" = "string2" ]]:
$ cat -v myscript
#!/bin/bash
for i in "${#:2}" ; do
if [[ "$2" = "$i" ]]
then
echo "$2 and $i are the same"
else
echo "$2 and $i are different"
fi
done
$ chmod +x myscript
$ ./myscript dummy target foo bar target
target and target are the same
target and foo are different
target and bar are different
target and target are the same
As you can see from this runnable example, it works. If you find that it doesn't on your system, you should provide a complete example like the above demonstrating it.
I would suggest
if [ "$2" = "$i" ]

Resources