Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Trying to make a shell script - need help

  • 23-05-2011 11:46am
    #1
    Registered Users, Registered Users 2 Posts: 3,739 ✭✭✭


    Hey lads... I'm just starting to use shell scripts but I'm far from great at it...

    I'm basically trying to write a script to take 3-4 files and compare the length of them and then output the name of the longest file...

    eg.. essay.txt music.txt document.txt

    then have the output as essay.txt as the essay is the longest file..

    I know I need to do some form of arguments such as $1 $2 and $3 but I'm really stuck at how to start these scripts...

    any help would be really appreciated lads..

    Thanks :)


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    ls -l | awk '{print $5 " " $9}'|sort -n| tail -1 |cut -d\  -f2
    
    ls -l # get the full listing of the files
    awk '{print $5 " " $9}' # extract the size and name
    sort -n # sort by size
    tail -1 # Take the last entry (largest file)
    cut -d\ -f2 # drop the size and print the filename

    There is one obvious problem here, if you don't shoot users who put spaces in filenames the displayed filename will be truncated at the first space.

    If you need to use this in a shell script that takes arguments try the following
    ls -l $* | awk '{print $5 " " $9}'|sort -n| tail -1 |cut -d\  -f2
    


  • Moderators, Arts Moderators Posts: 35,731 Mod ✭✭✭✭pickarooney


    There may be a slight difference in the result depending on whether you want the file with most words/lines or most bytes (e.g. a 500 line poem might be longer than a 400 line essay but have fewer words). If this is the case, maybe use wc -l instead of sort.

    Also, are all the files, and only those files, in the same directory or do you want to be able to specify the files to compare at runtime?


  • Registered Users, Registered Users 2 Posts: 3,739 ✭✭✭johnmcdnl


    when i'm running the script i just want to say.. sh script.sh /home/username/essay.txt /home/username/poem.txt etc tc

    I got it working anyways so thanks lads :)


  • Registered Users, Registered Users 2 Posts: 3,739 ✭✭✭johnmcdnl


    I'm not gonna make a new thread so I'm trying something new

    I want to write a sentence out and do some kind of command to find the longest word in the sentence...

    So say - script.sh My name is johnmcdnl

    will output "johnmcdnl"

    any ideas - once again thanks in advance guys


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    #!/bin/sh
    perl -e '$lenMax=0;for(@ARGV){if (length($_) > $lenMax){$string = $_ ;$lenMax=length($string);}}print "$string\n";' $*
    
    Sorry, but that's a bit much for a shell without creating a load of temp files etc...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,739 ✭✭✭johnmcdnl


    #!/bin/sh
    perl -e '$lenMax=0;for(@ARGV){if (length($_) > $lenMax){$string = $_ ;$lenMax=length($string);}}print "$string\n";' $*
    
    Sorry, but that's a bit much for a shell without creating a load of temp files etc...

    cheer mate :cool:

    I'm assuming that's the easiest way to do it.. I though there would be an easier way to do it but thanks anyways...


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    johnmcdnl wrote: »
    cheer mate :cool:

    I'm assuming that's the easiest way to do it.. I though there would be an easier way to do it but thanks anyways...

    'twas a sort of a joke, the more legible way to do it would be
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $long_word="";
    my $lenMax=0;
    for my $word (@ARGV){
       if (length($word) > $lenMax){
          $long_word = $word ;
          $lenMax=length($string);
       }
    }
    print "$long_word\n";
    


  • Registered Users, Registered Users 2 Posts: 1,065 ✭✭✭Snowbat


    The original problem...
    There is one obvious problem here, if you don't shoot users who put spaces in filenames the displayed filename will be truncated at the first space.
    I'd rather shoot the coder who didn't allow for perfectly valid filenames :p
    How about
    ls -S1 | head -n 1
    
    or
    ls -S1 'essay.txt' 'music.txt' 'filename with space.txt' | head -n 1
    



    Second problem...
    for word in 'one' 'thirty three' 'two' 'three' 'fourty four'; do echo `echo $word | wc -m` $word; done | sort -nr | head -n 1 | cut -d ' ' -f1 --complement
    
    for word in My name is johnmcdnl; do echo `echo $word | wc -m` $word; done | sort -nr | head -n 1 | cut -d ' ' -f1 --complement
    

    @johnmcdnl Do you need these in script form?


Advertisement