Announcement

Collapse
No announcement yet.

Bash scripts referencing $COLUMNS come up empty [SOLVED]

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Bash scripts referencing $COLUMNS come up empty [SOLVED]

    I'm writing a little wrapper script for a JPEG-to-ASCII program, and it defaults to 80 characters wide, so I'm trying to get it to read $COLUMNS to be as wide as the terminal.
    Unfortunately, $COLUMNS exists only in the terminal, scripts don't have it set, and I can't think of any way to get it from within the script.

    Several minutes of Google searches turned up only this, which isn't helpful.

    Here's the line in question:
    Code:
    java -jar ./jave5/jave5.jar image2ascii $@ "width=$COLUMNS"
    ...but, as I've said, $COLUMNS evaluates to nothing, so it ends up with "width=" and is ignored by the program.

    EDIT: Solution found. "tput cols" returns the number of columns.
    For external use only.

    #2
    Re: Bash scripts referencing $COLUMNS come up empty

    Hey Shee!

    Do you have access to Learning the Bash Shell published by O'Reilly (I have the 2nd Edition) or any other reputable book on Bash?

    In mine, page 77 deals with environment variables. On page 78:

    "Some environment variable names have been used by so many applications that they have become standard across many shell environments. These variables are not built into bash, although some shells, such as the Korn shell, have them as built-ins. Table 3-8 lists the ones you are most likely to come across."

    COLUMNS is one of the listed variables in the table. Back to page 77, it says "Any variable can become an environment variable. First it must be defined as usual; then it must be exported with the command:*

    export varnames

    (varnames can be a list of variable names separated by blanks).

    * Unless automatic exporting has been turned on by set -a or set -o allexport, in which case all variables that are assigned to will be exported.

    Don't know if this will help you any, and you may already know it, but you question allowed me an opportunity to get into my book (again).
    Windows no longer obstructs my view.
    Using Kubuntu Linux since March 23, 2007.
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      Re: Bash scripts referencing $COLUMNS come up empty

      I know about exporting, and that occurred to me, but exporting only affects child processes, and to export it, I'd need to be able to access it with a script, but I can only read it from an interactive terminal. That would be bad, because the whole point of writing the wrapper script is to simplify or automate what I have to do.

      So... anyone else have any ideas? Any programs that'll give info on term size? Anything?
      For external use only.

      Comment


        #4
        Re: Bash scripts referencing $COLUMNS come up empty

        Originally posted by SheeEttin
        ...so I'm trying to get it to read $COLUMNS to be as wide as the terminal...
        What are you meaning by 'terminal' in this case - the X session that is running (your desktop) or a console?
        Windows no longer obstructs my view.
        Using Kubuntu Linux since March 23, 2007.
        "It is a capital mistake to theorize before one has data." - Sherlock Holmes

        Comment


          #5
          Re: Bash scripts referencing $COLUMNS come up empty

          I'm going to be calling it from Konsole or Yakuake, and I want the image to fill the width of the terminal.
          So, I need to get the number of columns in the terminal somehow.

          I might also be calling it from a regular console (or through ssh), but that's not likely.
          For external use only.

          Comment


            #6
            Re: Bash scripts referencing $COLUMNS come up empty

            Okay, you're making me think (maybe good, maybe ... )

            Code:
            echo $COLUMNS
            from within a the following terminal sessions responds with the column width of the session window:

            Linux Console
            Screen Session
            Root Shell
            Shell

            Originally posted by SheeEttin
            I'm going to be calling it from Konsole or Yakuake,...I might also be calling it from a regular console (or through ssh), but that's not likely.
            From your comments, you will be running a shell ("I'm going to be calling it from Konsole or Yakuake,"), so access to the variable COLUMNS should be available within your script.

            Have you considered simply defining an environment variable from within your script (which is being called from Konsole or ...), such as WIDTH=$COLUMNS
            Windows no longer obstructs my view.
            Using Kubuntu Linux since March 23, 2007.
            "It is a capital mistake to theorize before one has data." - Sherlock Holmes

            Comment


              #7
              Re: Bash scripts referencing $COLUMNS come up empty

              Tried that. It disappears if you try it in a script. I stuck "echo $COLUMNS" in as the first line (and an "exit 0" after), but all I got out was a blank line.

              I suppose I could set it to use a static width dependant on the contents of the if $KONSOLE_DCOP variable, which is what I used to define special exit behavior for Yakuake... But that wouldn't do what I'm trying to do. It'd work, sure, but still...

              Oh, and forget what I said about other terminals. Apparently, it requires being run with an X server accessible.

              EDIT: Solution found. "tput cols" returns the number of columns.
              For external use only.

              Comment

              Working...
              X