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 08-11-2008, 02:45 AM   #1
TalkingMarble
Member
 
Registered: May 2008
Location: Deventer, The Netherlands
Distribution: CentOS 5.0
Posts: 50

Rep: Reputation: 17
Bash script : string problem


Hi all,

I'm new to bash scripting and have a little problem with the following script:

if [ $# -ne 0 ] && [ -d /vmware/$1 ]
then
vmxFile=`find /vmware/$1 -name "*.vmx"`
else
echo "Directory does not exist."
exit 1
fi

cd /scripts

./PowerOff.pl $1 $vmxFile

sleep 10

tar -cvzf $1.tgz /vmware/$1
if [ $? -eq 0 ]
then
if [ -e $1.tgz ]
then
lftp -c put $1.tgz -o ftp://10.11.71.25/VMwareBackups/
if [ $? -eq 0 ]
then
send_mail $1 "`hostname`: Backup Virtual Machine Successful"
rm $1.tgz
./PowerOn.pl $1 $vmxFile
else
send_mail $1 "`hostname`: Backup Virtual Machine Failed"
fi
fi
else
send_mail $1 "`hostname`: Backup Virtual Machine Failed"
fi

When i run the script the find command retrieves the name of the VMX file from the virtual machine to be stopped, and puts the name in the vmxFile string. The string contains spaces and when i pass string vmxFile as an argument to ./PowerOff.pl it treats the string like several small arguments instead of one big argument, causing ./PowerOff.pl to crash.

How can i accomplish that the string will be seen as one argument instead of several small ones?

Any help will be appreciated.

TIA
 
Old 08-11-2008, 03:20 AM   #2
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 8,559

Rep: Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098Reputation: 8098
Add double quotes around the argument:

./PowerOff.pl $1 "$vmxFile"

Eric
 
Old 08-11-2008, 03:25 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by TalkingMarble View Post
How can i accomplish that the string will be seen as one argument instead of several small ones?
Use quotes.

For example, here I write a function which prints out the arguments it receives, and then call it with quoted arguments to show how this affects commands. The same will apply to calling your script:

Code:
#!/bin/bash

arg_test () {
    n=1
    for arg in "$@"; do
        echo "arg $n is $arg"
        let n+=1
    done
    echo ""
    echo ""
}

echo "first time:"
arg_test one two three

echo "second time:"
arg_test "one two three"

echo "third time:"
arg_test one "two three"
output:
Code:
first time:
arg 1 is one
arg 2 is two
arg 3 is three


second time:
arg 1 is one two three


third time:
arg 1 is one
arg 2 is two three
 
Old 08-11-2008, 03:34 AM   #4
TalkingMarble
Member
 
Registered: May 2008
Location: Deventer, The Netherlands
Distribution: CentOS 5.0
Posts: 50

Original Poster
Rep: Reputation: 17
As suggested i surrounded the string with quotes, and the scripts now works as intended. Thank you all for the help.
 
  


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
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
Need bash script to get string from file marhen Programming 2 03-25-2008 09:01 AM
help with bash script: remove * to the last . in string drkstr Linux - Software 3 04-25-2006 04:54 PM
String manipulation with BASH script King V Programming 9 04-21-2006 03:15 PM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM

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

All times are GMT -5. The time now is 07:26 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
Open Source Consulting | Domain Registration