Errors in bash with if statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Errors in bash with if statements
# 1  
Old 10-10-2009
Errors in bash with if statements

Hello everyone,
I got this type of error when programming in bash
new.bat: 16: cannot create : Directory nonexistent
$bool
new.bat: 37: Syntax error: "then" unexpected (expecting "fi")
Does anyone know why?
Here is my code

#!bin/bash
#function helps(){
#echo "$ compile -o filename -clean -backup - archive -help cfilenames"
#echo "-o Filename : the mandatory argument that indicates the compiled executable name."
#echo "-clean : remove all.o files in backup"
#echo "-backup : copy all files in Backup folder from C"
#echo "-archive : tar the Source directory and move the tar to Backup folder"
#echo "-help : display this help information"
#}
s1='h'
s2='clean'
s3='backup'
s4='archive'
bool=''
echo $1 >File.txt
grep "o " File.txt > $bool
echo '$bool'
if [ bool = '' ];
then
rm -f -r File.txt
echo "Argument -o is mandatory"
echo "help menu"
echo "$ compile -o filename -clean -backup - archive -help cfilenames"
echo "-o Filename : the mandatory argument that indicates the compiled executable name."
echo "-clean : remove all.o files in backup"
echo "-backup : copy all files in Backup folder from C"
echo "-archive : tar the Source directory and move the tar to Backup folder"
echo "-help : display this help information"
fi
if [ bool != ''];
then
rm -f -r File.txt
echo "write the path of your C file please."
read filename
gcc filename $1
if[$s1=$1 -o $s1=$s2 -o $s1=$3 -o $s1=$4 -o $s1=$5];
then
echo "help menu"
echo "$ compile -o filename -clean -backup - archive -help cfilenames"
echo "-o Filename : the mandatory argument that indicates the compiled executable name."
echo "-clean : remove all.o files in backup"
echo "-backup : copy all files in Backup folder from C"
echo "-archive : tar the Source directory and move the tar to Backup folder"
echo "-help : display this help information"
fi
if[$s2=$1 -o $s2=$s2 -o $s2=$3 -o $s2=$4 -o $s2=$5];
then
cd ~/Projects/Store/Backup
rm -f -r *.o
fi
if[$s3=$1 -o $s3=$2 -o $s3=$3 -o $s3=$4 -o $s3=$5 -o $s3=$6];
then
cd ~/Projects/Store/Build1/Source/C
mv -f *.o *.h ~/Projects/Store/Backup
fi
if[$s4=$1 -o $s4=$2 -o $s4=$3 -o $s4=$4 -o $s4=$5 -o $s4=$6];
then
cd ~/Projects/Store/Build1/Source
tar -cvvf Source.tar Source/
fi
fi
# 2  
Old 10-10-2009
Either one of my contact lenses fell out, or that's a very small font!

Please repost with code-tags.
# 3  
Old 10-10-2009
Code:
echo '$bool'  should be echo "$bool"
if [ bool = '' ]; should be  if [ -z $bool ];
if[$s1=$1 -o $s1=$s2 -o $s1=$3 -o $s1=$4 -o $s1=$5];  should be if [ $s1 -eq $1 -o $s1 -eq $s2 -o $s1 -eq $3 -o $s1 -eq $4 -o $s1 -eq $5 ];

To keep the forums high quality for all users, please take the time to format your posts correctly.
  1. Use Code Tags when you post any code or data samples so others can easily read your code.
    You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)
  2. Avoid adding color or different fonts and font size to your posts.
    Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.
  3. Be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Reply With Quote
# 4  
Old 10-10-2009
Remove the semicolon after the if statement that is not require unlese you are keeping your then in the same statement -- remove all semicolon from the end of if statement and try

When you put a semicolon at the end of if --it mean the statement is complete there and - so in the next line when you say ' then' it will be like then with out if and hence the error

Last edited by jambesh; 10-10-2009 at 02:19 PM.. Reason: edit
# 5  
Old 10-10-2009
The script is actually littered with errors.

Code:
bool=''
echo $1 >File.txt
grep "o " File.txt > $bool
echo '$bool'
if [ bool = '' ];
then
rm -f -r File.txt
echo "Argument -o is mandatory"
echo "help menu"
echo "$ compile –o filename –clean –backup – archive -help cfilenames"
echo "-o Filename : the mandatory argument that indicates the compiled executable name."
echo "-clean : remove all.o files in backup"
echo "-backup : copy all files in Backup folder from C"
echo "-archive : tar the Source directory and move the tar to Backup folder"
echo "-help : display this help information"
fi
if [ bool != ''];
then
rm -f -r File.txt
echo "write the path of your C file please."
read filename
gcc filename $1
if[$s1=$1 -o $s1=$s2 -o $s1=$3 -o $s1=$4 -o $s1=$5];

This part alone has a bunch.

There is nothing wrong with the semi-colons.

Last edited by Scott; 10-10-2009 at 02:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

New bash menu printing errors but closes too quickly

I am beginning to write a new version of the bash menu below. The previous version worked great and now when I start the updated bash it opens and a some lines print in the terminal and it closes quickly. I know there are errors but how can I see them or fix them, I tried set -x with the same... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Equivalence of "if then" statements in bash

Would these two sections of code be equivalent? The original is the first one, followed by the new implementation. if ; Then ... else if ; Then ... fi fi if ; Then ... else if ; Then ... fi (4 Replies)
Discussion started by: kristinu
4 Replies

3. Ubuntu

Bash to Ash, errors and adjustments

I wrote Bash script and now I want to convert it to Ash. One headache is this function: do_adduser() { setaddprompt _arr_add=("Add manually" "Add via TXT" "return to main menu" "exit program") select add_action in "${_arr_add}" do case "$REPLY" in 1)... (7 Replies)
Discussion started by: fzivkovi
7 Replies

4. Shell Programming and Scripting

Converting from Linux bash (GNU) to Solaris script syntax errors

Original script written on CentOS 6.3 with GNU bash 4.1.2 Destination system is Solaris 9 with GNU bash 2.05 (not changeable by me) I have a script written on the linux side but now we need to provide a version to another site that "doesn't like linux". I've been going through changing the ] or... (13 Replies)
Discussion started by: oly_r
13 Replies

5. Shell Programming and Scripting

Bash script errors when executing

I'm working on a script that will search through a directory for MKV files and remux them to MP4. I found a few other scripts around the net that do this, but they all have limitations that don't always get the job done. The main limitation I've found is that most scripts look for the video track... (2 Replies)
Discussion started by: rayne127
2 Replies

6. Shell Programming and Scripting

Bash if statements in one line

I have the following bash code, and wondering if it is possible (and a good idea) to write the if statements in one line. # Run raytrac on the sound speed model. if ; then $bashPath/raytrac.bash -fcmd=$fcmd.cmd fi # Plot the travel times and ray paths. if ; then ... (8 Replies)
Discussion started by: kristinu
8 Replies

7. Shell Programming and Scripting

Multiple If statements in bash problem

Hi everyone, May you help me with the correct syntax of the follow bash statements please X=10 if ]; then echo "The value is between 1 and 5" for ((i=1;i<=${X})); do echo $i done else if ]; then echo "The value is between 6 and 10" for ((i=1;i<=${X})); do ... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

8. UNIX for Dummies Questions & Answers

Major OS errors/Bash errors help!!!!

Hi all, dummy here.... I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: [: =: unary... (12 Replies)
Discussion started by: wcmmlynn
12 Replies

9. UNIX for Advanced & Expert Users

Bash/Unix Command Errors

Hi all, I have major errors on entering the shell. On login I get: -bash: dircolors: command not found -bash: tr: command not found -bash: fgrep: command not found -bash: grep: command not found -bash: grep: command not found -bash: id: command not found -bash: # root@host # pwd /bin... (0 Replies)
Discussion started by: wcmmlynn
0 Replies

10. UNIX for Advanced & Expert Users

Adapter Errors and Link Errors

$ errpt | more IDENTIFIER TIMESTAMP T C RESOURCE_NAME DESCRIPTION 3074FEB7 0802050205 T H fscsi1 ADAPTER ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR B8113DD1 0802050205 T H fcs1 LINK ERROR 3074FEB7 0802050205 T H fscsi0 ADAPTER ERROR B8113DD1 ... (0 Replies)
Discussion started by: mcastill66
0 Replies
Login or Register to Ask a Question