DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 27th September 2011
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,051
Thanked 198 Times in 156 Posts
Default /bin/sh quoting problems

Can anyone explain to me why this doesn't work?

Code:
#!/bin/sh

#PS4=">>>> "
#set -xv

var="'This is label'"
ls  ${var}

[~/]% ./test.sh
ls: cannot access 'This: No such file or directory
ls: cannot access is: No such file or directory
ls: cannot access label': No such file or directory
From commandline (/bin/sh):
Code:
sh-4.2$ ls 'This is label'
This is label
I'm confused ... ... Turning on set -xv only served to increase my confusion

Code:
[~]% ./test.sh

var="'This is label'"
>>>> var=''\''This is label'\'''
ls  ${var}
>>>> ls ''\''This' is 'label'\'''
ls: cannot access 'This: No such file or directory
ls: cannot access is: No such file or directory
ls: cannot access label': No such file or directory
Exit 2
Where did all those quotes come from ?

The larger picture:

I want to build a variable with arguments for (c)dialog, i.e.:
# dialog --menu 'Hello world' 10 40 9 "This is Label" "This is text "

If I put "This is label" "This is text" inside a variable it doesn't work ...

(Of course, the real script has many more menu entries... )
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.

Last edited by Carpetsmoker; 27th September 2011 at 03:32 PM.
Reply With Quote
  #2   (View Single Post)  
Old 27th September 2011
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

Well it's simply wrong....
You need to surround ${var} with quotes, when you ls

Code:
ls "${var}"
Reply With Quote
  #3   (View Single Post)  
Old 27th September 2011
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,051
Thanked 198 Times in 156 Posts
Default

That works only for a single argument, not multiple:

Code:
var='This is label'
ls "${var}" # Works

var='This is label' 'This is text'
ls "${var}" # Oh noes!
What I need to have is something along the lines of:
# cdialog --menu 'Hello World' 10 40 9 'I am Label' 'I am Text' 'Another label' 'More text'

... Where the highlighted text should be ${var} ...

From the commandline this gives me:
Code:
┌──────────────────────────────────────┐
│ Hello World                          │
│ ┌──────────────────────────────────┐ │
│ │     I am Label     I am Text     │ │
│ │     Another label  More text     │ │
│ │                                  │ │
│ └──────────────────────────────────┘ │
├──────────────────────────────────────┤
│       <  OK  >    <Cancel>           │
└──────────────────────────────────────┘
If I wouldn't quote anything, then every word would be a label/text ... :-|

It sounds silly, but putting 'I am Label' 'I am Text' 'Another label' 'More text' inside of ${var} has kept both myself and a coworker busy for quite some time (With no resolution so far)...
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #4   (View Single Post)  
Old 27th September 2011
graudeejs's Avatar
graudeejs graudeejs is offline
Real Name: Aldis Berjoza
formerly killasmurf86
 
Join Date: Jul 2008
Location: Riga, Latvia
Posts: 588
Thanked 29 Times in 26 Posts
Default

sh isn't even close to perfect for text processing


Sorry, I can't help, cause I don't see the picture
Reply With Quote
  #5   (View Single Post)  
Old 27th September 2011
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 3,116
Thanked 182 Times in 149 Posts
Default

Code:
j65nko@hercules[~]cat dialog

var="\"This is label\" \"This is text\""
echo ${var}

j65nko@hercules[~]sh dialog
"This is label" "This is text"
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote
  #6   (View Single Post)  
Old 27th September 2011
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,051
Thanked 198 Times in 156 Posts
Default

j65nko:

Yes, this works when echoing, but not with cdialog:

Code:
[~]% cat test.sh
#!/bin/sh

var="\"I am Label\" \"I am Text\""

cdialog --menu 'Hello World' 10 40 9 ${var}

echo ${var}

[~]% sh test.sh
┌──────────────────────────────────────┐
│ Hello World                          │
│ ┌──────────────────────────────────┐ │
│ │          "I      am              │ │
│ │          Label"  "I              │ │
│ │          am      Text"           │ │
│ └──────────────────────────────────┘ │
├──────────────────────────────────────┤
│       <  OK  >    <Cancel>           │
└──────────────────────────────────────┘

"I am Label" "I am Text"
cdialog weirdness or cdialog weirdness? I suspect sh weirdness ...

@graudeejs Other tools aren't available in this environment and are not an solution in this case. Trust me, if it would be, I would use them.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #7   (View Single Post)  
Old 27th September 2011
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,051
Thanked 198 Times in 156 Posts
Default

More prodding about produced this, which seems to work. The tip came from this old stackoverflow question, I'm still confused about what's going on though

Code:
#!/bin/sh

cmd="cdialog --menu 'Hello World' 10 40 9 "

list="I am Label|I am Text
hello world|foo bar"
IFS="
"
for arg in ${list}; do
  label=$(echo ${arg} | cut -d '|' -f 1)
  text=$(echo ${arg} | cut -d '|' -f 2)

  cmd="${cmd} '${label}' '${text}'"
done

#echo ${cmd}
eval ${cmd}
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Reply

Tags
/bin/sh, bourne shell, quotes

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
three problems :) craze OpenBSD Packages and Ports 6 6th March 2011 03:26 PM
GDM Problems Saint OpenBSD Packages and Ports 4 29th August 2010 09:42 AM
startup (rc) problems kopcicle OpenBSD General 9 15th May 2010 08:35 PM
GCC causing problems it seems. Marred FreeBSD Ports and Packages 12 11th September 2008 08:11 AM
ATI DRI problems harisman FreeBSD General 16 11th May 2008 05:12 PM


All times are GMT. The time now is 05:00 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick