Solved

Bash Shell Arrays

Posted on 2013-01-11
8
317 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
[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
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 38

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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 

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 38

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 38

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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

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é.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

630 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