Results 1 to 10 of 10

Thread: A couple of BASH scripting questions

  1. #1
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default A couple of BASH scripting questions

    Ok I have a couple of BASH scripting questions, the first may have some obvious answer that I didn't think of.

    1. How do I test if a variable is empty? What I mean by this is use the read command to get input from the user, but allow for the user to just hit enter and a test checks the variable sees that it is empty and assignees a default value to it. I was thinking of something like this

    Code:
    #!/bin/sh
    echo "Enter some text [This is a test message]"
    read variable
    if [ $variable == "" ] ; then
        variable="This is a test message"
    fi
    echo $variable
    I have also tried with "-eq" instead of "==" and I get the following "/home/vendion/bin/test.sh: line 4: [: ==: unary operator expected"

    2. Is it possible count the number of matches that ls and grep gives, and sort the files and such from oldest to newest if it helps the files in question have the dates in the file names.
    "We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: A couple of BASH scripting questions

    You can either do:

    Code:
    if [ "$variable" = '' ]
    or

    Code:
    if [ -z "$variable" ]
    -eq is for integer comparision.

    As for your second question, something like:

    Code:
    ls | grep 'some re' | wc -l
    maybe?

    To sort by date:

    Code:
    ls -rt
    Normal -t sorting is newest first, -r reverses that.

  3. #3
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: A couple of BASH scripting questions

    However there is a shortcut for your situation where you want to print a default message if the variable is null or unset:

    Code:
    echo ${variable:-The variable is empty}
    See man bash, section "parameter expansion".

    For the test operators, see man test.

  4. #4
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default Re: A couple of BASH scripting questions

    Quote Originally Posted by ken_yap View Post
    However there is a shortcut for your situation where you want to print a default message if the variable is null or unset:

    Code:
    echo ${variable:-The variable is empty}
    See man bash, section "parameter expansion".

    For the test operators, see man test.
    Well I want to set the variable if empty to something default so the script can continue, instead of just saying that the variable is empty. Bash doesn't have support for anything like an array does it? That way once I have a list of the files from oldest to newest I can place them all in a array in that order and work with the files there instead of having to write a loop to work with each file one by one.
    "We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: A couple of BASH scripting questions

    You can also set a variable to a default if it's null or unset, it's all in the Parameter Expansion section of the man page for bash, but I'll save you the trouble of looking it up

    Code:
    ${variable:=Default value}
    Bash does support arrays, see the section Arrays in, oh did I mention it, the man page for bash.

    Code:
    $ foo[0]='hello'
    $ foo[1]='world'
    $ echo ${foo[1]}
    world
    $ echo ${foo[*]}
    hello world
    $ foo=('hello' 'world')
    $ echo ${foo[*]}
    hello world
    Then too, you might prefer to do your programming in something like Perl, Python or Ruby, it's just as easy to feed a list of file names into a script.

  6. #6
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default Re: A couple of BASH scripting questions

    If I was smart at the time I starting on this script I would have done it in perl or something, but at the time 1 didn't think about it 2 didn't know as much perl as I do now so Bash was a logical choice now I see where Perl would come in handy. I'm sure taking a list of file names and loading them in an array in the way I need will be 100 times easier in perl than it is in bash.

    Well thanks for the help and if I have anymore questions I will make the time to read the man pages before asking here.
    "We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper

  7. #7

    Default Re: A couple of BASH scripting questions

    Hi!

    This will probably work:

    Since you mentioned perl , you can use "defined" to do this test.

    For instance, in your example above, this code would probably work to get you started in the right direction too:

    Code:
    #!/usr/bin/perl -w
    
    print "Hi, enter some text!:";
        $text = <stdin>;
        chomp $text;
        exit unless $text =~ /\w+/;
        print "you entered $text!\n";
    Take care!

  8. #8

    Default Re: A couple of BASH scripting questions

    2. Is it possible count the number of matches that ls and grep gives, and sort the files and such from oldest to newest if it helps the files in question have the dates in the file names.
    Hey I didn't see this, well it's a little hard to understand, but maybe this is what you are trying to do?

    Code:
    #!/usr/bin/perl -w
    
    %filedb =();
    
            opendir (thisdir, ".") or die "Can't open CWD! $!";
                    while ($thefile = readdir(thisdir)){
                            next if $thefile =~ /^\./;
                            next unless -f $thefile;
                            $filedb{$thefile} = $thefile;
                            $filedb{$thefile}->{'age'} = -M $filedb{$thefile};
                    
                    }
    
             print "filename              age\n";
             print "-------------------------\n";
    
                    for $keys (sort(keys %filedb)){
                    print "$filedb{$keys}\t$filedb{$keys}->{'age'}\n";
                    }
    which when run doesn't recurse subdirectories, you can figure that out...here is sample, output, it's not pretty but I think might point you in the right direction! Good Luck!


    Code:
    filename              age
    -------------------------
    age.pl	0.000127314814814815
    test.pl	0.0716666666666667
    test_file	0.396643518518519
    test_file.bak	0.396643518518519
    test_file.bak.bak	0.397106481481481

  9. #9
    Join Date
    Jun 2008
    Location
    Cleveland, TN USA
    Posts
    412

    Default Re: A couple of BASH scripting questions

    Well I'm trying to avoid having to rewrite the whole thing into a different language. Ken_yap helped me with my BASH questions, but I don't think you had the second question quite right I was looking for a way to take the output of 'ls -l' and sort it.
    "We must plan for freedom, and not only for security, if for no other reason than only freedom can make security more secure." Karl Popper

  10. #10

    Default Re: A couple of BASH scripting questions

    Hi, yes the above code example is the same thing as ls -l sorted, based on the age of the files, with file times printed.

    By all means, please continue with BASH I figured since you mentioned perl you may be interested in exploring that option further! Good luck

Posting Permissions

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