LinuxQuestions.org
Review your favorite Linux distribution.
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
  Search this Thread
Old 08-09-2017, 07:47 AM   #1
inderpal2406
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Rep: Reputation: Disabled
bash scripting error related to echo command


Hi,

My script is as follows:


#!/bin/bash
#This script will extend the specified VG.
clear;
printf "Please enter the name of the vg that you want to extend: \t";
read VG_NAME;
echo "$VG_NAME will be extended.";
printf "Please enter the no. of new disks that will be assigned: \t";
read DISK_COUNT;
echo "You have entered $DISK_COUNT number of disks.";
for (( c=1; c<=$DISK_COUNT; c++ ))
do
printf "Enter disk name: \t";
read DISK_$c;
done
echo "You have entered following disk names:";
for (( c=1; c<=$DISK_COUNT; c++ ))
do
echo ${DISK_$c};
done

OUTPUT:
Please enter the name of the vg that you want to extend: vg_a
vg_a will be extended.
Please enter the no. of new disks that will be assigned: 3
You have entered 3 number of disks.
Enter disk name: a
Enter disk name: b
Enter disk name: c
You have entered following disk names:
./vg_extend.sh: line 18: ${DISK_$c}: bad substitution


I am unable to echo the individual disk names. Please help !


NOTE: I had executed the script in source shell to verify if variable DISK_1, DISK_2, DISK_3 are being initiated or not. They are
getting initiated and holds the value as a, b, c respectively. But the problem is I am unable to display them in for loop.

BR,
Inderpal
 
Old 08-09-2017, 08:18 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,228
Blog Entries: 3

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
It's not possible to do indirect variables quite that way in bash. It's done a bit differently:

http://tldp.org/LDP/abs/html/ivr.html
 
1 members found this post helpful.
Old 08-09-2017, 08:43 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,943

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
In addition, take a look at using arrays.

http://www.linuxjournal.com/content/bash-arrays
 
1 members found this post helpful.
Old 08-09-2017, 10:34 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,417
Blog Entries: 6

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

The link supplied by Turbocapitalist above is what you need to most directly adapt your existing code. You may also find it in the bash man page under Parameter Expansion, indirect expansion.
 
2 members found this post helpful.
Old 08-09-2017, 10:39 AM   #5
inderpal2406
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
It's not possible to do indirect variables quite that way in bash. It's done a bit differently:

http://tldp.org/LDP/abs/html/ivr.html


Hi,

I used the arrays in bash and it is working now. Thanks for your answer.

BR,
Inderpal
 
Old 08-09-2017, 10:41 AM   #6
inderpal2406
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
In addition, take a look at using arrays.

http://www.linuxjournal.com/content/bash-arrays

Hi,

I used the bash arrays in following way and it is working now. Thanks

Script:
-bash-4.3$ cat vg_extend.sh
#!/bin/bash
#This script will extend the specified VG.
clear;
printf "Please enter the name of the vg that you want to extend: \t";
read VG_NAME;
echo "$VG_NAME will be extended.";
printf "Please enter the no. of new disks that will be assigned: \t";
read DISK_COUNT;
echo "You have entered $DISK_COUNT number of disks.";
for (( c=1; c<=$DISK_COUNT; c++ ))
do
printf "Enter disk name: \t";
read arr[$c];
done
echo "You have entered following disk names:";
for (( c=1; c<=$DISK_COUNT; c++ ))
do
echo ${arr[$c]};
done


Output:
Please enter the name of the vg that you want to extend: vg_gorkon
vg_gorkon will be extended.
Please enter the no. of new disks that will be assigned: 2
You have entered 2 number of disks.
Enter disk name: /dev/disk/disk248
Enter disk name: /dev/disk/disk249
You have entered following disk names:
/dev/disk/disk248
/dev/disk/disk249


BR,
Inderpal
 
Old 08-09-2017, 10:47 AM   #7
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,228
Blog Entries: 3

Rep: Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010Reputation: 1010
Quote:
Originally Posted by michaelk View Post
In addition, take a look at using arrays.

http://www.linuxjournal.com/content/bash-arrays
Great. Now they're on One of Those Lists if they clicked on that link.

Seriously though, LJ is a useful resource.
 
Old 08-09-2017, 10:49 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 15,943

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Oh well. I'm sure that I am on several by now...
 
  


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
Bash Scripting: Echo to Standard Error colonel_t Linux - Newbie 6 03-03-2014 08:58 AM
[SOLVED] bash scripting - read command and input echo armandino Linux - General 2 01-24-2011 10:24 AM
information related to echo command mohit_parihar Linux - General 2 12-01-2009 07:06 AM
bash scripting question, homework related cybergeek11235 Linux - General 4 10-12-2008 10:59 PM
Bash Scripting - echo command question gbhil Programming 2 04-27-2005 07:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:58 AM.

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