Solved

Bash Shell Arrays

Posted on 2013-01-11
8
315 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

752 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