Solved

Bash Shell Arrays

Posted on 2013-01-11
8
310 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
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
 

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 37

Expert Comment

by:Gerwin Jansen
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
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now