?
Solved

how to pass arguments with spaces in shell script

Posted on 2013-06-21
5
Medium Priority
?
860 Views
Last Modified: 2013-06-21
Hi experts,

Could you help me with the problem. I'm trying to pass argument value with spaces from script1.sh to script2.sh

I'm using getopts

Script output
==========
[Ravee.Kumaravelu.NEC-AUMBS-25775] ¿  ./script1.sh -u scott -p tiger -h "192.22.22.22 192.22.22.33"
-u was triggered, Parameter: scott
-p was triggered, Parameter: tiger
-h was triggered, Parameter: 192.22.22.22 192.22.22.33
user is : scott
user pwd is : tiger
host server : 192.22.22.22 192.22.22.33
Invoking script2.sh with argument -u scott -p tiger -h "192.22.22.22 192.22.22.33"
Inside script2.sh
-u was triggered, Parameter: scott
-p was triggered, Parameter: tiger
-h was triggered, Parameter: 192.22.22.22
user is : scott
user pwd is : tiger
host server : 192.22.22.22



Attached the script for your quick reference.

Thanks in advance
script1.sh
script2.sh
0
Comment
Question by:enthuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:comfortjeanius
ID: 39265698
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39265700
Change in script1.sh:

./script2.sh -u $user  -p $pwd -h "$host"

Please note the double quotation marks around "$host"!
0
 

Author Comment

by:enthuguy
ID: 39265770
thanks friends,

hi  comfortjeanius, can you provide a link which as some sample

HI woolmilkporc, surrounding with quotes didnt help
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 1400 total points
ID: 39265786
>> HI woolmilkporc, surrounding with quotes didnt help <<

Here is the output I'm getting by running your scripts with the mentioned modification:

# ./script1.sh -u scott -p tiger -h "192.22.22.22 192.22.22.33"
-u was triggered, Parameter: scott
-p was triggered, Parameter: tiger
-h was triggered, Parameter: 192.22.22.22 192.22.22.33
user is : scott
user pwd is : tiger
host server : 192.22.22.22 192.22.22.33
Invoking script2.sh with argument -u scott -p tiger -h "192.22.22.22 192.22.22.33"
Inside script2.sh
-u was triggered, Parameter: scott
-p was triggered, Parameter: tiger
-h was triggered, Parameter: 192.22.22.22 192.22.22.33
user is : scott
user pwd is : tiger
host server : 192.22.22.22 192.22.22.33

What else did you expect?
0
 

Author Closing Comment

by:enthuguy
ID: 39265835
Hi woolmilkporc, my apologies, I updated the echo line and tested

working as expected. thanks very much!
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month9 days, 2 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question