LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-05-2010, 01:38 AM   #1
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
What is the equivalent of this bash script in sh script?


Below is a script for dynamic variable creation

Code:
#!/bin/bash

variableName="test"
variableVal="123"
eval ${variableName}=`echo -ne \""${variableVal}"\"`

echo $test #"test" variable created dynamically using eval
When I execute this with the first line set to #!/bin/sh, the result is
eval: 1: 123: not found

But when I execute it as is above noting #!/bin/bash, it works as desired and the result is
123

I am wondering what is the difference, and what is the equivalent of this script if it is written in sh.

Thanks!

Regards,
archieval
 
Old 10-05-2010, 02:11 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,208

Rep: Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799Reputation: 1799
Out of curiosity, have you changed the name to something other than 'test' which in itself is a command that eval may have evaluated?
 
Old 10-05-2010, 02:30 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,375

Rep: Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911Reputation: 1911
The problem is that the echo built-in in the Bourne Shell does not accept options, so that the command substitution results in a literal
Code:
-ne "123"
hence the error from eval, which tries to evaluate the expression:
Code:
test=-ne "123"
You can either remove -ne from the echo call or simplify you script using a simple variable substitution (actually I don't see any utility for the echo statament in this case):
Code:
eval ${variableName}=${variableVal}
Hope this helps.
 
Old 10-05-2010, 03:09 AM   #4
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
Ah so that's the reason for the error. I just copied that script from a certain website, not knowing that it can be simplified because I do not know the meaning of "echo -ne xxx", so I execute it as is. Hey this really helps and a new knowledge! Thanks!

Regards,
archieval
 
  


Reply

Tags
bash scripting, dynamic, variable


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 09:16 AM
Bash script problem with ftp session exiting the script early edomingox Programming 5 02-23-2010 05:39 AM
Is 'nice' inherited to child processes? e.g. bash script/php script that calls MySQL SirTristan Linux - Newbie 1 12-04-2008 12:57 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM


All times are GMT -5. The time now is 03:23 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration