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
Related
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. :(
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
I have this shell script which takes 2 parameters but when i run this it shows me this error.What do I need to do to write this if-then-else block correctly?
NK_BILLED_UNBILLED_DATA.sh: line 31: syntax error near unexpected token else'
'K_BILLED_UNBILLED_DATA.sh: line 31: else
Shell Script
echo Script Name: "$0"
echo Total Number of Argument Passed: "$#"
echo Arguments List -
echo 1. $2
if test -d $2;then
echo "Welcome $2"
else
echo "Not Exist";
fi
echo All Arguments are: "$*"
ORACLE_SID=BSCS01U
export ORACLE_SID
export PATH=.:$PATH:$ORACLE_HOME/bin
export BC=$1
echo "$BC"
if [ $# -ne 0 ];then
export REPORT_GEN_PATH=$2;
else
export REPORT_GEN_PATH=$WORK/Reports/ANT001A_REQ01/generated_reports;
fi
export REPORT_HOME=$BSCS_WORKDIR/Reports/ANT001A_REQ01
echo "Please wait....The Billed and Unbilled report is being created."
sqlplus SYSADM/SYSADM#$ORACLE_SID << eof_disp > $REPORT_HOME/logs/log_file_$$.log
#$REPORT_HOME/scripts/GEN_BILLED_UNBILLED_REPORT_FILE.sql
eof_disp
echo "Billed and Unbilled report created."
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'
I recently started writing BASH scripts, and I am currently trying to practice using while loops. However, when I run the following block of code, the command prompt responds with:
run.command: line 12: syntax error near unexpected token `done'
run.command: `done'
Then the program shuts off.
This is the code I am running.
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
i=read
if [$i = "exit"]; then
exit
else if [$i = "no"]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done
I did some research on while loops, however my loop syntax seems correct. When I remove the done at the end, and Unexpected end of file error occurs. Any help would be appreciated!
You can use the -p option of read for the prompt and a case ... esac construction:
while true; do
read -r -p ">> " i
case "$i" in
"exit") exit 0 ;;
"no") echo "no" ;;
*) echo -e "Error: $i is undefined";;
esac
done
I fixed it myself!
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
read i
if [ "$i" = "exit" ]; then
exit
elif [ "$i" = "no" ]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done