LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2011, 03:31 AM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
Bash Scripts - need help with While and If


Hi all,

Could anyone help me with the following BASH script?

Code:
 
i=0
  while [ $i -lt $ARRAYLEN ]; do
    if ["$META1" = "$array"];
      then 
        META1FLAG=1
      else
        META1FLAG=0
    fi
  let i++
done
While $array contains a word like "start"

When I run the script, In the terminal I either get the response:

Quote:
myscript.sh: line 3: [: missing `]'
or if $META1 contains the same word "start, I get:

Quote:
myscrit.sh: line 3: [start: command not found

The purpose of the script is to evaluate a parameter that a user might include when they invoke my script, and compare it to some data that $META1 might contain.

If they match I want to set a flag and later launch Xine. If they do not match, I want to set a flag to zero and do something else.

I'm a bit lost as to what the responses are trying to tell me in the terminal window when I run this script. Not sure what I am doing wrong.

Last edited by greengrocer; 03-10-2011 at 03:32 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-10-2011, 03:39 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Code:
i=0
  while [ $i -lt $ARRAYLEN ]; do
    if [ "$META1" = "$array" ];
      then 
        META1FLAG=1
      else
        META1FLAG=0
    fi
  let i++
done
You need spaces in between your brackets and quotation marks.

Cheers,

Josh
 
2 members found this post helpful.
Old 03-10-2011, 03:46 AM   #3
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
You need spaces in between your brackets and quotation marks.
Ahhh thanks. LOL Too funny that I missed something like that. What a newb I am.
 
Old 03-10-2011, 03:52 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Anything else you need help with?
 
Old 03-10-2011, 06:18 AM   #5
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by corp769 View Post
Anything else you need help with?
There might be. I've put the spaces where they need to be and have integrated the above little bit of script into the rest of the script I've been working on.

There are still a couple of bugs I will try to iron out tomorrow. So I may need some more help at that point if I get stuck.
 
Old 03-10-2011, 09:33 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
Glad you solved the first issue. I would point out that using a variable name such as 'array' and the fact that the loop is looking at 'ARRAYLEN' I am kind of surprised that there are no arrays
anywhere in the code presented? ie nothing except the value of 'i' is changed through the loop so i would imagine the answer is the same for all iterations.

Just an observation of course
 
Old 03-10-2011, 11:42 AM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by grail View Post
Glad you solved the first issue. I would point out that using a variable name such as 'array' and the fact that the loop is looking at 'ARRAYLEN' I am kind of surprised that there are no arrays
anywhere in the code presented? ie nothing except the value of 'i' is changed through the loop so i would imagine the answer is the same for all iterations.

Just an observation of course
Noticed that too....
 
Old 03-11-2011, 06:16 AM   #8
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
$array gets its content/values before the snippet I presented is run. Likewise, $META1 gets its content/value from the first piece of meta data found on a given line inside a text file containing many lines, before the snippet I presented is run.

Basically when the user invokes myscript.sh, they can specify a parameter or two (or more) with it. If the parameter matches the meta data, the script will do something. If no match, the script will choose another line from the file and check for a match.

Last edited by greengrocer; 03-11-2011 at 06:19 AM.
 
Old 03-11-2011, 06:27 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899Reputation: 2899
But just to confirm, the variable 'array' is not actually an array but just a string?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where are BASH commands stored? Not scripts but what bash exe uses. theKbStockpiler Programming 11 02-23-2011 04:06 PM
KDE 4 Autostart Program Bash Script to Write Autostart Bash Scripts for You! jdmcdaniel3 SUSE / openSUSE 1 05-03-2010 07:17 AM
[SOLVED] Finding bugs in bash scripts, Analyis tool for bash traene Programming 2 10-31-2009 12:42 PM
I need some help with BASH scripts. BoB4ik Programming 10 12-20-2007 05:06 PM
Bash scripts? BajaNick Programming 3 07-05-2003 11:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration