LinuxQuestions.org
Review your favorite Linux distribution.
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 11-18-2011, 06:44 AM   #1
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Rep: Reputation: Disabled
quick shell script question


Trying to make my first shell script here, but already having problems. The first 4 lines give an error saying "1: MAP: not found" and so, although on many tutorial pages variable declaration goes just like that.
Code:
MAP = " -> ../../"
NAME_1 = "ST3250823AS_5ND145H9"MAP
NAME_2 = "2B020H1_B1LJZS9E"MAP
NAME_3 = "2B020H1_B1JDS2RE"MAP

id_1 = `ls -l /dev/disk/by-id | grep $NAME_1`
id_2 = `ls -l /dev/disk/by-id | grep $NAME_2`
id_3 = `ls -l /dev/disk/by-id | grep $NAME_3`

echo $id_1
echo $id_2
echo $id_3
approximate Java version of what I want to do:

Code:
public static final String MAP = " -> ../../";
public static final String NAME_1 = "ST3250823AS_5ND145H9" + MAP;
public static final String NAME_2 = "2B020H1_B1LJZS9E" + MAP;
public static final String NAME_3 = "2B020H1_B1JDS2RE" + MAP;

String id_1 = Linuxshell.execute("ls -l /dev/disk/by-id | grep '" + NAME_1 + "'");
String id_2 = Linuxshell.execute("ls -l /dev/disk/by-id | grep '" + NAME_2 + "'");
String id_3 = Linuxshell.execute("ls -l /dev/disk/by-id | grep '" + NAME_3 + "'");
System.out.println(id_1);
System.out.println(id_2);
System.out.println(id_3);

Last edited by hui; 11-18-2011 at 06:57 AM.
 
Old 11-18-2011, 07:12 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
In bash, there is no space either side of the = operator so you need MAP=" -> ../../"

Reason is bash is an automated command shell and the "no spaces" is an easy way to avoid problems with variables having the same name as a command.
 
Old 11-18-2011, 07:36 AM   #3
hui
LQ Newbie
 
Registered: Jun 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
In bash, there is no space either side of the = operator so you need MAP=" -> ../../"

Reason is bash is an automated command shell and the "no spaces" is an easy way to avoid problems with variables having the same name as a command.
that was totally the problem, ty.
 
Old 11-18-2011, 07:59 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
BTW ...

To add the value of MAP to the end of the NAME_* variables, you need something like
Code:
NAME_1 = "ST3250823AS_5ND145H9"$MAP
It may help to think of $ as a dereference operator or as meaning "the value of".

Backticks for command substitution are not wrong but the later $( <command> ) form is preferred for reasons explained here.

A convention, widely but not universally used, is to reserve uppercase variable names for environment variables.
 
Old 11-18-2011, 04:22 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You really shouldn't post multiple threads concerning the same basic topic. Keep related things all in one place.

I just posted two useful comments to your other thread.

http://www.linuxquestions.org/questi...g-grep-914235/
 
  


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
[SOLVED] Quick shell question: rename Steve W Linux - Newbie 8 03-28-2011 12:18 PM
quick variable question in a shell script yargk Linux - Newbie 4 02-23-2010 03:09 PM
quick shell question kebabbert Solaris / OpenSolaris 10 10-27-2009 07:31 PM
Quick Shell Question youssefe2k Linux - Software 2 04-10-2005 10:47 PM
I need a quick shell script. 360 Programming 3 03-22-2002 05:44 PM

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

All times are GMT -5. The time now is 08:44 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