My if statement executes both if's rather than just 1 [closed] - bash

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 6 years ago.
Improve this question
So I'm learning Bash and I have no idea why my if statement isn't working (working in putty if it makes any difference). I've tried looking online for how an if statement is supposed to be built and followed it making sure I got all the details correct. When I run it both of the if's come out positive and both of them get executed rather than just 1.
echo -n "Enter file name: "
read x
echo "file $x with numbers (Y/N)?"
read y
if [ "$y"="n" ];
then
cat $x
fi
if [ "$y"="y" ];
then
cat -n $x
fi
exit
can anyone help please?

Remember there needs to be a whitespace in your if condition else it becomes an assignment. https://uvesway.wordpress.com/2013/03/11/some-whitespace-pitfalls-in-bash-programming/

"$y"="n" is neither a conditional test nor an assignment. It's just a single string e.g, if $y is 'y', then "$y"="n" is the same as the literal 'y=n' and this literal string is being treated as a single parameter to the test ([) command.
A condition like below is always evaluated to true:
if [ abcd ]; then echo true; fi
while an empty string makes it false:
if [ '' ]; then echo true; else echo false; fi
White spaces aren't optional (everywhere) in bash. Even though the [ abcd thingy looks like a syntax, it's not; [ is a command (test) and abcd is its first positional parameter.

Related

Bash script to find factorial [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 1 year ago.
Improve this question
The program takes input but show the following errors, how to resolve those errors ?
[ is not mere syntax, it's a command, therefore it needs space to separate it from its arguments.
while [ $counter -le $number ]
# .....^....................^
Make sure you validate that number is actually only digits.
Some other comments:
Use $(...) instead of `...` -- see
https://github.com/koalaman/shellcheck/wiki/SC2006
for more details.
bash can do arithmetic, you don't need to call out to expr. See
Arithmetic Expansion
and Shell Arithmetic
in the manual.
There is also an arithmetic conditional construct (analogous to the
string-oriented [[...]] conditional construct) -- see
Conditional Constructs
and scroll down to ((...)) (there's no direct link).
I think you need to add a space after [ and before ]:
#!/bin/bash
echo "Enter the number to find it's factorial"
read number
total=1
counter=1
while [ $counter -le $number ];
do
total=` expr $counter \* $total`
counter=` expr $counter + 1`
done
echo $total
Working here on Ubuntu:
$ ./factorial.sh
Enter the number to find it's factorial
5
120
Actually, on line 7, you forgot the completing backtick.

Test with no operator? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
A simple question :
What is the advantage of a test using a test operator versus one directly testing the variable?
As follows :
[ -n "$foo" ] && echo "that's some variable :"$foo || echo '$foo is blank.'
versus
[ $foo ] && echo "that's some variable :"$foo || echo '$foo is blank.'
The second is more elegant. But I suspect there must be a reason, because someone went to the trouble of making "-z" and "-n" exist.
I suspect [ "$foo" ] (quote the parameter expansion) is supported for historical reasons; once upon a time, saving two bytes by omitting the explicit, commonly used, -n could be significant.
Today, I would always use -n explicitly for clarity.
Note that you should not combine && and || for a "one-line" conditional statement. Use an if statement.
if [ -n "$foo" ]; then
echo "that's some variable: $foo"
else
echo "foo is blank."
fi

need help understanding bash syntax [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to understand bash syntax a bit better, and I need some help with a while loop:
The following script works:
#!/bin/bash
#
counter=2
mystring=testdir
while [ $counter -le 5 ]; do
echo Making dir $mystring$counter
mkdir $mystring$counter
ls *.slurm > $mystring$counter/testfile.$counter.slurm
counter=$((counter+1))
done
Question 1: what is -le ? Google didn't seem to help, showing me any page with the word 'linux' in it
I literally copied the following form the while manual, and it does not work:
#!/bin/bash
set x 0
while {$x<10} {
puts "x is $x"
incr x
}
#test3.sh: line 6: syntax error near unexpected token `}'
#test3.sh: line 6: `}'
Question 2: What am I doing wrong? Thank you for your attention :)
The [ is a synonym for test builtin function and -le is one of the possible tests, lower or equal. In bash try this for more details:
help [
help test
Re. your second question, it doesn't look like bash syntax at all.
what is -le ?
it means "less than or equal".
What am I doing wrong?
Almost everything. Your script should look like this in bash
x=0
while [ $x -lt 10 ]; do
echo "x is $x"
((x++))
done
I think you really should read the bash manual.

Write a script to get the line number of a file with "while read LINE", but failed [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
#!/bin/sh
num=1
cat $1 | while read LINE
do
num=`expr $num + 1`
done
echo $num
Your script is spawning a sub-shell when you use pipe after useless cat. All the changes to $num made inside sub-shell get lost after while loop ends and you get back to parent shell.
You should initialize num with 0 not 1
It is better to not to use all capital letter variable names to avoid collision with internal shell variables.
Instead of reverse tick you should use $(...) for command substitution.
You should use:
#!/bin/sh
num=0
while read -r line
do
num=$(expr $num + 1)
done < "$1"
echo $num

Unix Shell scripting query on sed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Need a help on unix shell scripting. As I am new to this.
I have a shell script written. While running that script am giving argument as say 003. I need to replace this argument value in a particular line in shell scripts as below.
Script:
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q=WMS.XXX.vinoth
I need to replace XXX value with 003 and append into a temp file. Can you please help me???
Thanks in advance!!!
Do you ask how to replace the XXX with first argument?
Q=WMS.$1.vinoth
Using the $1 will work, however be aware that you should probably use quotation marks when passing a number like 003 in stead of 3 as in some cases the two zeros in front might be dropped.
Also I recommend wrapping the string in quotation marks as well, avoiding accidental command calls.
./script "003"
if [[ $# != 1 ]]
then
echo "Please enter the Value"
echo "eg: script.sh 003"
exit 0;
fi
Q="WMS.$1.vinoth"

Resources