You are not logged in.

#1 2010-08-14 12:39:49

marfig
Member
From: Portugal
Registered: 2010-07-30
Posts: 189
Website

[SOLVED] Variable expansion issues (bash)

Hello all,

I'm setting up a script to switch .Xdefaults files depending on whether I'm starting Openbox or i3. But I'm having a problem with a variable expansion

Here's the script that starts X

  1 # home/marfig/bin/startx
  2 #!/bin/sh
  3 
  4 # Set env for openbox or i3. Later processed by .xinitrc
  5 case "$1" in
  6 'i3')
  7         WM="i3 -V >>$HOME/.i3/i3log >&1"
  8         ln -sf ~/.Xdefaults.i3 ~/.Xdefaults
  9 ;;
 10 *)
 11         WM="openbox-session"
 12         ln -sf ~/.Xdefaults.openbox ~/.Xdefaults
 13 ;;
 14 esac
 15 
 16 # Run wm
 17 export WM
 18 /usr/bin/startx "$2"

The problem I'm having is with the first $WM declaration.

When the X startup process reaches .xinitrc, $WM will expand to "/home/marfig/i3 -V >>home/marfig/.i3/i3log >&1", which is obviously not what I want. i3 executable doesn't reside in my home folder.
If however, I just declare WM="i3", this expansion doesn't occur and i3 launches correctly (i think, from /usr/bin/).
I'm new to bash. So, I'm not really sure what's happening or where to look.

Here's .xinitrc:

  1 #!/bin/sh
  2 #
  3 # ~/.xinitrc
  4 #
  5 # Executed by startx (run your window manager from here)
  6 
  7 # Initiate vmware integration
  8 vmware-user&
  9 
 10 # Execute wm
 11 if [ -n "${WM+x}" ]; then
 12         exec "$WM"
 13 else
 14         exec openbox-session
 15 fi

My PATH environment variable is set to $HOME/bin:$PATH in .bashrc.

Last edited by marfig (2010-08-14 13:51:02)


I probably made this post longer than it should only because I lack the time to make it shorter.
- Paraphrased from Blaise Pascal

Offline

#2 2010-08-14 13:05:31

MrX
Member
From: Bavaria
Registered: 2010-07-10
Posts: 9

Re: [SOLVED] Variable expansion issues (bash)

Maybe this helps:

WM="`i3 -V >>$HOME/.i3/i3log >&1`"

But I'm not an expert ;-)

Offline

#3 2010-08-14 13:13:04

rwd
Member
Registered: 2009-02-08
Posts: 664

Re: [SOLVED] Variable expansion issues (bash)

What makes you conclude that WM gets expanded to '"/home/marfig/i3  ...'? And what happens if you add the full path like
WM="/usr/bin/i3 ....."?

Offline

#4 2010-08-14 13:26:11

marfig
Member
From: Portugal
Registered: 2010-07-30
Posts: 189
Website

Re: [SOLVED] Variable expansion issues (bash)

rwd wrote:

What makes you conclude that WM gets expanded to '"/home/marfig/i3  ...'?

X doesn't launch i3 and exits with the error:

/home/marfig/.xinitrc: line 12: /home/marfig/i3 -V >>/home/marfig/.i3/i3log >&1: No such file or directory
/home/marfig/.xinitrc: line 12: exec: /home/marfig/i3 -V >>/home/marfig/.i3/i3log >&1: cannot execute: No such file or directory

And what happens if you add the full path like
WM="/usr/bin/i3 ....."?

I get a similar error:

/home/marfig/.xinitrc: line 12: /usr/bin/i3 -V >>/home/marfig/.i3/i3log >&1: No such file or directory
/home/marfig/.xinitrc: line 12: exec: /usr/bin/i3 -V >>/home/marfig/.i3/i3log >&1: cannot execute: No such file or directory

But I know for a fact /usr/bin/i3 exists and if I simple declare WM="i3", i3 launches successfully.
/home/marfig/.i3/i3log is also a valid path

Last edited by marfig (2010-08-14 13:27:42)


I probably made this post longer than it should only because I lack the time to make it shorter.
- Paraphrased from Blaise Pascal

Offline

#5 2010-08-14 13:37:58

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: [SOLVED] Variable expansion issues (bash)

The problem is the inclusion of the redirection in the variable. Bash does not recognize the redirection later when it expands the variable, which is why it's telling you that it can't execute "/usr/bin/i3 -V >>/home/marfig/.i3/i3log >&1"... there is no executable on your system named "/usr/bin/i3 -V >>/home/marfig/.i3/i3log >&1".

Create a script to handle the redirection and then launch that instead, e.g.


/home/marfig/bin/start.i3

#!/bin/bash
/usr/bin/i3 -V >>$HOME/.i3/i3log >&1

/home/marfig/bin/startx

  'i3')
    WM="/home/marfig/bin/start.i3"
    ln -sf ~/.Xdefaults.i3 ~/.Xdefaults
  ;;

Last edited by Xyne (2010-08-14 13:39:58)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2010-08-14 13:40:27

marfig
Member
From: Portugal
Registered: 2010-07-30
Posts: 189
Website

Re: [SOLVED] Variable expansion issues (bash)

I've been looking at the Bash Reference Manual (3.7.3 Command Execution Environment) and in the second case, when I add /usr/bin) it works as expected. It's not so clear anymore why it then says it can't execute an existing file.

On the case I don't add /usr/bin, I have no idea why it expands to my home folder when there's no i3 file in there.

On the case I just declare as "i3", it works as expected. So I'm pretty sure this is an issue with the way I'm trying to pass the parameters. I just don't know how to fix it.

EDIT:

Thank you xyne smile

That worked. Didn't see your answer when I started typing this. But saw it now.

Last edited by marfig (2010-08-14 13:43:28)


I probably made this post longer than it should only because I lack the time to make it shorter.
- Paraphrased from Blaise Pascal

Offline

Board footer

Powered by FluxBB