how to execute if statement line by line in bash - bash

how to execute if statement line by line in bash??
for i in $(cat $install_script)
do
if [ "$i" = "$check" ]; then
count=$((count+1))
echo whiptail --title "Install SmartBoard" --yesno "${scripts_name[$count]}" 7 $((${#scripts_name[$count]} + 5))
if (( $? == 0 )); then
skip=false
else
skip=true
fi
fi
if [ "$i" == "$check wifi" ]; then
break
fi
if [ $skip == true ]; then
continue
else
sh -c $i ## process line by line
fi
done
error !!
sh: 1: Syntax error: end of file unexpected (expecting "then")
sh: 1: Syntax error: "then" unexpected
sh: 1: Syntax error: "else" unexpected
sh: 1: Syntax error: "fi" unexpected
i'm not good english so if my posts word be rude Sorry that. :(

Related

How to fix Bash script errors

I wrote a little Bash script called cs210_list.sh, and it is giving me the following errors when i run it either as bash cs210_list.sh or as
sh cs210_list.sh in linux:
cs210_list.sh: line 2: [0: command not found
cs210_list.sh: line 21: syntax error near unexpected token `fi'
cs210_list.sh: line 21: ` fi'
this is my very first bash script ever. I can't figure why I'm getting the errors. BTW, "(line 21)" is not actually in my code, it's just there for your reference.
$? -eq 0 This command is supposed to determine if there as a argument
#!/bin/bash
if [$? -eq 0];
then
echo "Error: Missing argument";
exit 1
fi
if [-e "$1"];
then
if [-d "$1"];
then
for i in $(ls)
do
if [-d "$i"];
then
echo "File: $i"
echo ""
else
echo "File: $i"
echo " Size: " $(stat -c%s $i)
fi
fi (line 21)
else
echo "$Error: File doesn't exist"
fi
done

if and && concept in shell script

I am new to Shell script , I am trying to run a simple code
Input for the test.sh file will be input1 and input2 (For Example ./test_script.sh input1 input2)
#!/bin/bash
export a=$1
export b=$2
if ["$a" -eq ''] && ["$b" -eq '']
then
echo "Please enter an input"
elif ["$a"] && ["$b" -eq '']
then
echo "the value of a is $a"
elif ["$a"] && ["$b"]
then
echo "The value of a is $a and b is $b"
else
print "Something went wrong "
fi
Is this syntax correct because I am getting an error , Need help to find where I am going wrong
./test.sh 10 11
./test.sh: line 5: [10: command not found
./test.sh: line 10: [10]: command not found
./test.sh: line 14: [10]: command not found
./test.sh: line 19: print: command not found

Getting an error when I nest a for-loop inside an if-else statement

I am trying to write a simple bash script with a for-loop inside an if-else statement. The following code works:
if [ $# -le 0 ]; then
echo 'No arguments provided.'
else
echo "Arguments detected."
fi
But as soon as I replace the second echo with a for-loop, it breaks:
if [ $# -le 0 ]; then
echo 'No arguments provided.'
else
for i in "$#"
do
echo "Arguments detected: $i"
done
fi
I've tried to execute only the for-loop and I'm given an error, so I think the for-loop is wrong somehow, but I don't know how. The error is:
bash: C:/Users/Angleton/.bashrc: line 45: syntax error near unexpected token `fi'
bash: C:/Users/Angleton/.bashrc: line 45: ` fi'

bash script error unexpected token error

if [ $USER=root ]; then echo "hi" else echo "bye" fi
it is giving me the following error
line 5: syntax error near unexpected token `fi'
line 5: `fi'
The correct code:
if [ $USER == "root" ]; then echo "hi; else echo "bye"; fi

Bash script - syntax error near unexpected token `then'

I have this simple bash script that keeps failing with the following messages:
./servo.sh: line 7: syntax error near unexpected token then'
./servo.sh: line 7: if[ "$level" -eq 1 ]; then'
And my bash script:
#!/bin/bash
level=1
while :
do
if[ $level -eq 1 ]; then
echo "hello"
else
echo "else"
fi
done
What am I doing wrong?
add a space between the if and the [

Resources