Solved

Bash Shell Arrays

Posted on 2013-01-11
8
312 Views
Last Modified: 2013-01-11
So I'm in the process of making a kiosk system using a LiveCD on a USB key, and I've gotten jsut about everything working save one thing; setting up multiple monitors if they exists.  By default, X wants to clone the monitors, where I need them in a spanning mode, so I am writing a small script to go in the xinitrc that will do this for me.  I'm not great with BASH, and I'm having problems with the arrays and hoping someone can point me on the right path.  here's my script code:

#!/bin/bash

declare -a OUTPUTS
OUTPUTS=($(xrandr -q | grep " connected" | cut -c -6))
EXECUTE=""

EXECUTE="xrandr --output "${OUTPUTS[0]}" --auto"

if [[ ${#OUTPUTS[@]} -gt 1 ]]; then
      ARRLEN=${#OUTPUTS[@]} for ((i=1;i<$ARRLEN;i++)); do
                              EXECUTE+=" --output "${OUTPUTS[${i}]} " --auto --right-of "${OUTPUTS[${i} - 1]}
                done
fi

echo $EXECUTE
0
Comment
Question by:MonMusc
8 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 38767554
Other than the lack of a newline or ; between the  ARRLEN assignment and the for loop,
and the space between ${OUTPUTS[${i}]} and " --auto --right-of ",
 what problems are you having?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38768222
You should escape these " here:

EXECUTE="xrandr --output \"${OUTPUTS[0]}\" --auto"
0
 
LVL 19

Expert Comment

by:simon3270
ID: 38768811
One possible problem is that you are taking the first 6 chars on the "connected" lines - I tested this on my machine and the first 6 chars were
    VGA1 c
(i.e two words, not one).  I assume you are only interested in the "VGA1" bit.  Replace
  cut -c -6
with
  OUTPUTS=($(xrandr -q | awk '/connected/{print $1}'))

What is the value of $EXECUTE at the end of the script?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:MonMusc
ID: 38768841
So I was getting the error:
Line 10: --auto --right-of DVI-0: command not found.

It then hit me, I had a space after the first variable reference and before the quotation mark.  Get rid of the space and guess what, the dang thing worked!

EXECUTE+=" --output "${OUTPUTS[${i}]} " --auto --right-of "${OUTPUTS[${i}
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38769036
And where was that space exactly? I don't see it when I compare your last comment to you original post.
0
 

Author Comment

by:MonMusc
ID: 38769047
{i}]} "
0
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38769051
Yep :)
0
 

Author Comment

by:MonMusc
ID: 38769199
One little mistake, but that's what I get for being used to C# and Visual Studio.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS - HAProxy- KeepAlived 5 43
Linux Ubuntu - Volume "boot" has only 0 bytes disk space remainig 6 64
wipe a usb using python 5 48
winscp 000webhost.com 6 54
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

810 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