Comparing Korn Shell Variables

Posted on 2012-09-14
Last Modified: 2012-09-14
Is there a simple way to compare the value of two Korn shell variables, and when the value of the second is larger, assign that value to the first? So, if the value of VAR2 is greater than the value of VAR1, set VAR1 equal to VAR2.
Question by:babyb00mer
    LVL 68

    Expert Comment

    For a numeric comparison:

    [[ $VAR2 -gt $VAR1 ]] && VAR1=$VAR2

    For a string comparison:

    [[ "$VAR2" > "$VAR1" ]] && VAR1="$VAR2"

    "man test" explains all test operators.

    Author Comment

    Yep, that would do it. Do I feel stupid. I guess I was just feeling lazy today. :-)
    LVL 68

    Accepted Solution

    Don't worry, it's Friday!



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
    Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    755 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

    26 Experts available now in Live!

    Get 1:1 Help Now