Solved

Bash Shell Arrays

Posted on 2013-01-11
8
314 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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…
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.

740 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