Find the answer to your Linux question:
Results 1 to 3 of 3
Code: #!/bin/bash touch xrandr xrandr -q > xrandr #place xrandr in file, works for (( VAR=0 ; ; VAR++ )) do TMP=$(cat xrandr | awk 'NR==$VAR { printf $2 }') ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    8

    Reading file (output from xrandr -q)

    Code:
    #!/bin/bash
    touch xrandr
    xrandr -q > xrandr #place xrandr in file, works 
    for (( VAR=0 ;  ; VAR++ )) 
    do
    	TMP=$(cat xrandr | awk 'NR==$VAR { printf $2 }') #null?
    	TMP1=$(cat xrandr | awk 'NR==$VAR { printf $1 }')  #null?
    	if [ "$TMP"=="" ]; then break #EOF
    		elif [ "$TMP"~="connected" ]; then #new display
    			rm $TMP1 #remove previous file
    			touch $TMP1
    			TMP2=$TMP1 #set path for storing resolutions
    		elif [ "$TMP2"!="" ]; then echo "$TMP1 " >> $TMP2
    	fi
    done
    I'm trying to make several files: each named after the display and containing resolutions. But for some reason I get null when trying to read lines. Also, I'm noob.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    The problem has to do with your awk scripts.

    Although you have defined a variable called VAR, this variable is a Bash variable, not an awk variable. Therefore, in your awk script, it looks for the varaible VAR, sees that it doesn't exist, and treats $VAR as $0, which isn't what you want.

    You can do something like:
    Code:
    TMP=$(cat xrandr | awk -v "VAR=$VAR" 'NR==VAR { printf $2 }')
    This way, you have defined the awk VAR variable to be the same as the Bash $VAR variable.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    8
    Code:
    CONNECTED=$(cat xrandr | awk -v "VAR=$VAR" 'NR==VAR { printf $2 }')
    Code:
    cat: No such file or directory
    What I'm missing? (tested with single quotes as well)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...