LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-05-2013, 05:56 PM   #1
Cobozco
LQ Newbie
 
Registered: May 2013
Posts: 6

Rep: Reputation: Disabled
String Manipulation And Assigning


I've been writing bash programs and come up with the following code to echo the resolution of the display:

xdpyinfo | grep 'dimensions'

This is the output:

dimensions: 1280x1024 pixels (339x271 millimeters)

At the moment I have a user input of 1280x1024 being read and assigned to a variable for later use. I've been trying to figure out code for the program to not require a user input and just extract the 1280x1024 (note this number may change between monitors and the program should cover all circumstances)...I've been experimenting with tr, but nothing successful yet. Any help is much appreciated.
 
Old 06-05-2013, 06:28 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Quote:
Originally Posted by Cobozco View Post
At the moment I have a user input of 1280x1024 being read and assigned to a variable for later use. I've been trying to figure out code for the program to not require a user input and just extract the 1280x1024 (note this number may change between monitors and the program should cover all circumstances)...I've been experimenting with tr, but nothing successful yet.
(top of my head so may contain errors: YMMV(VM):
Code:
#!/bin/bash
# Set debug mode when testing:
set -Cvxe
# Set default behaviour:
LANG=C; LC_ALL=C; export LANG LC_ALL

# Make a list of binaries to check:
for BINARY in xdpyinfo xrandr; do
 # See if we have the required binary:
 which $BINARY >/dev/null 2>&1
 # If we have it (exit value 0)
 RETVAL=$?
 if [ $RETVAL -eq 0 ]; then
  # Each binary has its own syntax:
  case $BINARY in
   xdpyinfo)
             # Here we use an array to cut down on using external
             # binaries where it isn't necessary:
             DIMENSIONS=($(xdpyinfo | grep dimensions:)); 
             DIMENSIONS=${DIMENSIONS[1]}; break
             ;;
     xrandr) 
             # ...but here laziness seems the right choice ;-p
             DIMENSIONS=$(xrandr -q|awk '/^*/ {print $2"x"$4}')
             break;;
  esac
 fi
done

# Once done we check if the variable holds a value (or see 'man test'):
if [ ${#DIMENSIONS} -eq 0 ]; then
 echo "Invalid dimension, exiting."; exit 1
else
 # And only if it's not empty echo it:
 echo "Screen dimensions are: ${DIMENSIONS//x/ by }."
fi

exit 0

BASH intros:
http://www.gnu.org/software/bash/man...ode/index.html
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

BASH scripting guides:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
# these are must reads so do read them!

Common questions / problems:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.orgDontReadLinesWithFor
http://mywiki.wooledge.org/UsingFind
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

The Advanced BASH scripting guide:
http://www.tldp.org/LDP/abs/html/

Bourne shell (compatibility):
http://www.grymoire.com/Unix/Sh.html

Also see:
http://www.linuxquestions.org/questi...l-links-35334/
http://www.linuxquestions.org/questi...nd-line-32893/
http://www.linuxquestions.org/questi...ks-4175433508/
 
1 members found this post helpful.
Old 06-05-2013, 08:51 PM   #3
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,166
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Quote:
Originally Posted by unSpawn View Post
Code:
...
...
set -Cvxe
set -x I know, but can you elaborate the other switches for the uninitiated?
or a link would be good also, I can ask for elaborations after I RTFM.

I didn't see them in man bash but I did see this:
Code:
All of the  single-character shell options documented in the description of the  set  builtin  command
       can  be used as options when the shell is invoked.  In addition, bash interprets the following options
       when it is invoked:
or did I miss the mark?

Thanks!
 
1 members found this post helpful.
Old 06-05-2013, 11:57 PM   #4
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 495

Rep: Reputation: 212Reputation: 212Reputation: 212
Here's another approach:

Code:
$ TEST=$(xdpyinfo | grep -o '[0-9]\+x[0-9]\+ pixels')
$ echo ${TEST% pixels}
1920x1080
 
1 members found this post helpful.
Old 06-06-2013, 08:47 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
And if you need the width/height separately, or combined:

Code:
$ xwininfo -root | awk '/Width:/ { print $2 }'
1920
$ xwininfo -root | awk '/Height:/ { print $2 }'
1080
$ xwininfo -root | awk '/-geometry/ { print $2 }' | sed s/+0+0$//
1920x1080
 
1 members found this post helpful.
Old 06-06-2013, 11:13 AM   #6
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
Quote:
or a link would be good also
From one uninitiate to another :-)
The first BASH intro link that unSpawn posted, then '6.1 Invoking Bash', then 'The Set Builtin', leads to this. http://www.gnu.org/software/bash/man...he-Set-Builtin
 
1 members found this post helpful.
Old 06-06-2013, 01:16 PM   #7
Cobozco
LQ Newbie
 
Registered: May 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
@unSpawn/Habitual - Thanks for the code and the links...the code sort of goes over my head unfortunately, but the comments do help
@Diantre/jpollard - Thanks, I've be trying the code, and they're great - thanks
@allend - Thanks for link!
 
Old 06-06-2013, 03:49 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Code:
$ IFS=' x' read -r _ h w _ < <( xdpyinfo | grep 'dimensions' )
$ echo "$h $w"
1280 1024
string manipulation in Bash
 
1 members found this post helpful.
Old 06-06-2013, 09:05 PM   #9
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
Code:
$ IFS=' x' read -r _ h w _ < <( xdpyinfo | grep 'dimensions' )
That elegant solution makes me cringe at my own ignorance!
 
1 members found this post helpful.
Old 06-07-2013, 07:57 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Hehe. Don't cringe at it, just learn! I only learned it myself relatively recently.

People just don't usually think of using read to do string splitting, always thinking in terms of awk and the like.

The link I posted has lots of other useful techniques like this.

OTOH, I see that I got my height and width variables reversed.
 
Old 06-07-2013, 08:05 AM   #11
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,430

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
Quote:
OTOH, I see that I got my height and width variables reversed
I thought it was horizontal and wertical.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation C++ miamagoo Programming 6 07-31-2010 01:55 PM
string manipulation jtag Linux - General 9 02-01-2010 09:07 AM
Assigning custom string to std::string in C++ jh7777 Programming 4 10-13-2009 01:02 AM
String manipulation in C xeon123 Programming 2 10-17-2007 02:17 PM
String manipulation Manuel Alvarado Linux - Newbie 2 09-06-2006 10:49 PM


All times are GMT -5. The time now is 08:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration