LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-30-2010, 06:44 AM   #1
danbopes
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Rep: Reputation: 0
Passing variables to a program in a bash script


Trying to do a simple bash script, and having some issues. I am relatively new to bash, and did my best to search, but idk what exactly to search for.

Doesn't work:

Code:
DIALOG="1 test"
dialog --menu "Choose OS for install:" 10 30 1 ${DIALOG}
Works:
Code:
dialog --menu "Choose OS for install:" 10 30 1 1 test
Just trying to figure out how to tell it it's two different arguments and not one. I'm sure this is an issue with escaping, but I'm not sure how to fix it.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-30-2010, 06:56 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well some more information would be nice, like when you are executing the lines above, are they all being done at the prompt or from a script?
This worked fine for me (at prompt):
Code:
$ x="10 30 1 1 test"
$ dialog --menu "Choose OS for install:" $x
 
Old 12-30-2010, 07:06 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The quoted code using ${DIALOG} should have been functionally equivalent to the other quoted code. Can you give some detail on "doesn't work"?
 
Old 12-30-2010, 07:08 AM   #4
danbopes
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Code:
DIALOG="10 30 1 1 test"
dialog --menu "Choose OS for install:" $DIALOG
Gives me:
Error: Expected at least 6 tokens for --menu, have 2.

I am trying to run this through a script (as noted in the title, and in my post).
 
Old 12-30-2010, 07:20 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
This is very mysterious; $DIALOG seems to be working as "$DIALOG". Please try this at the command prompt and in a script and post the output
Code:
DIALOG="1 test"
for word in $DIALOG 
do
    echo $word
done
You will get a > prompt after the for command when entering it at the command prompt and until you enter the final done.
 
Old 12-30-2010, 07:40 AM   #6
danbopes
LQ Newbie
 
Registered: Dec 2010
Posts: 5

Original Poster
Rep: Reputation: 0
ok, I figured out what the problem was (1 of them anyway). I was using IFS to read a variable (newlines and stuff), and I never unset it after, so that solved one problem.

Here is my second problem (instead of making a new thread, cause it goes along with the title anyway).

Code:
#!/bin/bash

DIALOG="1 \"test with spaces rawr\""
dialog --menu "Choose OS for install:" 10 70 1 $DIALOG
It's not seeing the quotes when I do that, so it's reading it as 5 arguments and not 2. (1, "test, with, spaces, rawr") not (1, "test with spaces rawr")

Last edited by danbopes; 12-30-2010 at 07:42 AM.
 
Old 12-30-2010, 07:50 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
That is as-designed behaviour. Shell Operations details the mechanism that leads to such behaviour.

You can do what is intended by that code by using an array:
Code:
DIALOG=(1 "test with spaces rawr" )
dialog --menu "Choose OS for install:" 10 70 1 "${DIALOG[@]}"
 
2 members found this post helpful.
  


Reply



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 Script - Passing command string between variables Devcon Programming 13 01-10-2011 10:13 AM
Passing Environment variables to a shell-script kregec05 Programming 6 08-18-2009 08:27 AM
Passing cmake variables in a src2pkg script... Lufbery Slackware 24 08-05-2008 02:21 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Passing external variables/arguments to SQL program ganninu Programming 12 11-25-2003 05:30 AM

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

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