LinuxQuestions.org
Help answer threads with 0 replies.
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 04-05-2016, 02:52 PM   #1
seh640
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Rep: Reputation: Disabled
Newbie Linux Scripting


Hello,

I'm new to Linux and scripting. I'm trying to write a short script to increase some RAM on virtual machines. I'm getting an error:

"syntax error near unexpected token `do"

It looks like I have the syntax correct. Can anybody advise on what I'm doing wrong? Here is the code snippet.

#!/bin/bash
for i in "$vmList"; do
increaseMem=`xe vm-memory-limits-set name-label="$i" static-min=3221096448 dynamic-min=3221096448 dynamic-max=3221096448 static-max=322109644`
echo "$i has been upgraded to 3GB of RAM"
done
 
Old 04-05-2016, 03:20 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
What value does $vmList have?

In a for loop you're telling it to operate on a list of items.

If $vmList has been populated with space separated elements (e.g. ralph billy bob) putting quotes around it like "$vmLIst" is treating it as a single item rather than multiple items (e.g. the 3 I listed) so there is only one pass in which it would output:
ralph billy bob has been upgraded...

If you take out the double quotes it would treat them as multiple items so it would output:
ralph has been upgraded...
billy has been upgraded...
bob has been upgraded...

Also as an FYI you should get in the habit of using $(<command line>) syntax rather than `<command line>`. While both work the latter is deprecated and the former has two benefits over the former:
1) It isn't confused with single quotes like backticks can be so it makes it easier to read your code.
2) The $() is easier to nest.

So instead of:
Code:
increaseMem=`xe vm-memory-limits-set name-label="$i" static-min=3221096448 dynamic-min=3221096448 dynamic-max=3221096448 static-max=322109644`
Do:
Code:
increaseMem=$(xe vm-memory-limits-set name-label="$i" static-min=3221096448 dynamic-min=3221096448 dynamic-max=3221096448 static-max=322109644)
 
Old 04-05-2016, 04:18 PM   #3
seh640
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the advice. Here is the full code:


vmList=`xe vm-list params=name-label | cut -f2 -d':'`
echo ""

for i in "$vmList"
do
increaseMem=`xe vm-memory-limits-set name-label="$i" static-min=3221096448 dynamic-min=3221096448 dynamic-max=3221096448 static-max=322109644`
echo "$i has been upgraded to 3GB of RAM"
sleep 3
done

So I will remove those quotes around vmList and all should be well?
 
Old 04-06-2016, 09:05 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
Quote:
Originally Posted by seh640 View Post

So I will remove those quotes around vmList and all should be well?
Assuming the command below is outputting multiple white space separated items, yes. I can't test this command myself.
Code:
xe vm-list params=name-label | cut -f2 -d':'

To my other point I'd suggest modifying your code to eliminate `` as follows:
Code:
vmList=$(xe vm-list params=name-label | cut -f2 -d':')
echo ""

for i in $vmList
	do
	increaseMem=$(xe vm-memory-limits-set name-label="$i" static-min=3221096448 dynamic-min=3221096448 dynamic-max=3221096448 static-max=322109644)
	echo "$i has been upgraded to 3GB of RAM"
	sleep 3
done

Last edited by MensaWater; 04-06-2016 at 09:24 AM.
 
Old 04-06-2016, 09:11 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Note that:

Code:
echo ""
can be replaced with no loss of functionality by:

Code:
echo
 
Old 04-06-2016, 09:46 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,608

Rep: Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363Reputation: 4363
please use [code]here comes your script[/code] to keep formatting.
Also please add set -xv at the beginning of your script to see what's happening (that will not solve anything just you will see how your code works)
 
Old 04-06-2016, 01:50 PM   #7
seh640
LQ Newbie
 
Registered: Apr 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you all who replied. My issue has been resolved with the help on this thread. My issue was having the semi colon after the start of the for loop and having $vmList in quotes as suggested in a previous reply.

for i in "$vmList"; do

Thanks!
 
Old 04-06-2016, 02:20 PM   #8
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
Quote:
Originally Posted by seh640 View Post
TMy issue was having the semi colon after the start of the for loop..."
Glad you got it solved. However, just a note: The semi colon wouldn't be a problem. It is a common way of doing a one liner when you are doing a command line:

That is to say doing something like:
Code:
vmList=$(ls -d test*); for i in $vmlist; do echo I is $i; done
Is just as valid as doing:
Code:
vmList=$(ls -d test*)
for i in $vmList
do echo I is $i
done
 
Old 04-06-2016, 03:22 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
If vmList is set to be used only once and increaseMem set but never used, you might simplify your script that way:


Code:
echo
for i in $(xe vm-list params=name-label | cut -f2 -d':'); do
	xe vm-memory-limits-set name-label="$i" static-min=3221096448 dynamic-min=3221096448 dynamic-max=3221096448 static-max=322109644 && echo "$i has been upgraded to 3GB of RAM"
	sleep 3
done
 
  


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



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

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