Find the answer to your Linux question:
Results 1 to 6 of 6
Help, I need to write a script that prints the longest filename in a directory. The directory is supplied as an argument to the script. I have no problems supplying ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    Bash Scripting



    Help,

    I need to write a script that prints the longest filename in a directory. The directory is supplied as an argument to the script. I have no problems supplying and checking the argument, but am a bit lost after that. I tried the following

    for file in "$( find $1 -size +0)"
    do
    strlen=${#file}

    if [ "$strlen" -gt "$filelen" ]; thne
    filelen=$strlen
    filename=$file
    fi
    done

    echo "Filename is $filename"

    can anyone help, in desperation

    Drew

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Hi,

    This would do the job:

    Code:
    #!/bin/sh
    
    if [ -z "$1" ]
    then
      echo "Usage: `basename $0` directory-name"
      exit 1
    fi
    
    cd $1
    
    let "maxlen=0"
    
    for line in *
    do
      strlen=`echo ${#line}`
      if [ -f "$line" ]
      then 
        strlen=${#line}
        if [ $maxlen -lt $strlen ]
        then 
          let "maxlen=strlen"
          longestfilename=$line
        fi
      fi
    done
    
    echo $longestfilename : $maxlen
    
    exit 0
    Check the man page for the test command and look through a couple of scripting tutorials:

    http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
    http://tldp.org/LDP/abs/html/

    Any doubts? Feel free to ask more questions.
    And... your code would be easier to read if you placed it in CODE blocks!

    Regards

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    More Bash scripting

    Hey all,

    I need to find the starting character to a string provided as an argument to a script.

    I thought that this would work:

    firstch=`grep -e ^.{1} --label='hello'`

    Im using the literal 'hello' to test the command

    echo $firstch

    I want firstch to equal 'h'


    thanks in advance

    Drew

  4. #4
    Just Joined!
    Join Date
    May 2007
    Posts
    3

    Arrow

    I sorted the previous one, I'm trying to check all the supplied arguments

    while [ $currentarg -le $$numofargs ]; do
    if [ ! -f ${$currentarg} ]; then
    echo "File does not exist"
    exit 1
    else
    ( $currentarg ++ )
    fi
    done

    What is wrong with the variable substitution?

    if [ ! -f ${$currentarg} ]; then

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Are you trying to test the parameters of your script? And are you expecting directory names as parameters? The -f operator returns true if the substituted variable is a regular file.

    To test how variable substitution or whatever works, write a little script like this:
    Code:
    #!/bin/sh
    
    currentarg=$1
    
    if [ ! -f "$currentarg" ]; then
      echo $currentarg is not a regular file
    fi
    Use shift to read the next parameter within a loop, have a read of this:

    http://www.tldp.org/LDP/Bash-Beginne...ect_09_07.html

    If you want to really learn it, it is suggested that you learn by reading and experiencing it yourself.

    And place questions about scripting in the "Linux Programming & Scripting" section.

    Regards

  6. #6
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    448
    if "hello" is a file containing its own name, these work OK:

    firstch=`cut -c 1 <hello`

    firstch=`dd bs=1 count=1 2>/dev/null <hello`

    firstch=`awk '{print substr($0,1,1)' <hello`
    the sun is new every day (heraclitus)

Posting Permissions

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