Basic if else scripting [duplicate] - linux

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Command not found when comparing string
(1 answer)
"Command not found" when attempting integer equality in bash
(3 answers)
Why does [$value -lt 10] in shell result in command not found? [duplicate]
(3 answers)
Closed 4 years ago.
I hope you all are doing great. I am new to Linux shell scripting. I am trying to learn basic shell scripting and i started with with if else condition and I am stuck in it since a week. I have read as many articles as I could but I am not understanding what is the problem here. I know this must a very basic for you guys but please help me :)
Here is the script that I wrote
#!/bin/sh
count=102
if [$count -gt 100]
then
echo "$count is greater than 100"
else
echo "$count is less than 100"
fi
When I execute this script, I am getting below error.
./count.sh: line 5: [102: command not found
102 is less than 100
*I also tried taking user input not just with integer but with string also, still getting the same error. please help :)
Thank you in advance

you need to provide space after [ and before ]. For e.g
[ $count -gt 100 ]
Sample code :
count=102
if [ $count -gt 100 ]
then
echo "$count is greater than 100"
else
echo "$count is less than 100"
fi

Related

shell script looping [duplicate]

This question already has answers here:
How to loop through dates using Bash?
(10 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 11 months ago.
I am new to bash scripting and trying to write a loop to run a curl command but however getting errors. Please suggest the write syntax.
#!/usr/bin/env sh
start = $1
end = $2
start=$(date -d $start +%Y-%m-%d)
end=$(date -d $end +%Y-%m-%d)
while [[ $start -le $end ]]
do
echo $start
curl -H "command, which can't be shared"
start=$(date -d"$start + 1 day" +"%Y-%m-%d")
done
The command line to run the script used sh filename.sh "2022-02-01" "2022-02-25".
However, the first error I received is windows cannot find '2022-02-01'. Make sure you typed the name correctly, and then try again.
Please suggest the errors.

How to validate user input on a bash script? [duplicate]

This question already has answers here:
Validate the number of arguments passed in bash from read
(2 answers)
Closed 2 years ago.
I'm creating a bash script. However, I need the script to validate that the user has to enter two arguments. Not 1 and nothing more than 3.
echo -n "Enter two values"
read val1 val2
try this:
#!/bin/bash
if [ $# -eq 2 ]; then
echo "var1: $1 var2: $2"
else
echo "error: need to set two argument"
fi
run:
./scriptName.sh first second

Bash Linux - call function in a if statement [duplicate]

This question already has answers here:
if, elif, else statement issues in Bash
(5 answers)
Closed 3 years ago.
i have probably some nooby question
how can I call a defined function in a if statement in bash?
Have a look on my example:
#!/bin/bash -x
Variable_A=xyz
function_x() {
echo "hello everyone"
}
#here I want to define a if statement after the function
if Variable_A == working;then
function_x()
#here I have a while for the parameters in bash example:
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--status)
Variable_A="$2"
shift
shift
;;
esac
done
so the idea is when I run the script let's call it test.sh it runs the script exp in bash:
./test.sh working
I am not sure how to create something like this, maybe some of you can help me out on this.
Thank you in advance
Your if should look like:
#here I want to define a if statement after the function
if [ "$Variable_A" = "working" ]
then
function_x
fi
You need to end every if statement with fi and functions are called without ().
https://www.thegeekdiary.com/bash-if-loop-examples-if-then-fi-if-then-elif-fi-if-then-else-fi/
Another important note on if statements. You need a space before and after [ or ].
Your if logic should also be after the script parms are read and Variable_A is set.

get " [: if-then: unexpected operator ",when searching all sub-folder in a folder [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
when I run this script in my Ubuntu:
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
I got this output:
output
I don't understand why I get "[: if-then: unexpected operator"
Escape your variables. Read about it here.
for item in *
do
if [ -d "$item" ]
then
echo "$item"
fi
done

Check if a file exists in a linux script [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Closed 4 years ago.
i made a linux script which receives as first argument a path to a directory. I don't know the path. And i want to check if "file.txt" exists at that certain path . For example :
if [ -e $1/file.txt ];then
echo HAHA
fi
if [[ -e "$1/file.txt" ]]; then
echo "It exists"
fi

Resources