Find the answer to your Linux question:
Results 1 to 5 of 5
Hello everyone! Funny thing happened to me... I had a script I worked on and one day my HD crashed and I lost it... (yes I know I should of ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    7

    Shell script help - almost got it...

    Hello everyone!

    Funny thing happened to me... I had a script I worked on and one day my HD crashed and I lost it... (yes I know I should of had a backup lol). So now I'm trying to recreate this script but I can't seem to remember how I managed to get a certain function (command) to work... Maybe someone can help me out... Here are the details:

    I have the following block of script...

    Code:
    #!/bin/bash
    
    sn=5
    var1=`echo $1 |wc -L`
    res=$(($sn-$var1))
    
    echo -n $1 > tmp1
    COUNTER=1
    
    while [ $COUNTER -lt $res ]
    do
    printf " " >> tmp1
    let COUNTER+=1
    done
    Basically what this will do is if $1 passed to the script from is for example: "John" it will calculate and add 1 white space after "John" into the tmp1 file.

    Now my question is, what if $1 was more than 5 characters? Say for example: "Amanda" That would be 6 characters. What my script, originally did, was just take the first 5 characters if the word was longer than (in this case the sn= variable) 5. So the result in the tmp1 file would be "Amand".

    I vaguely remember using "sed" to accomplish this, but I could be wrong...

    Can someone shed some light on how I can possibly do this? Man I can't believe I lost the damn script lol

    Thanks in advance!!

    Scorp

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    You may have used sed to do this, but there is a simpler way, built into Bash directly.

    It's called a substring. We can specify that we only want the first five characters:
    Code:
    #!/bin/bash
    
    sn=5
    var=${1:0:$sn}
    
    linelen=`echo "$var" |wc -L`
    res=$(($sn-$linelen))
    
    echo -n "$var" > tmp1
    COUNTER=1
    
    while [ $COUNTER -lt $res ]
    do
        printf " " >> tmp1
        let COUNTER+=1
    done
    The bolded lines are lines that I changed. Basically, ${var:start:len} takes a substring of length len from starting index start (indices are 0-based). So here, I say that I want the $sn characters beginning at position 0. And I get only them.

    Does that make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    7

    confused?

    Hey

    Thanks for the reply! Much appreciate it... I'm a little confused...

    see my comments below:

    Code:
    #!/bin/bash
    
    sn=5
    var=${1:0:$sn} linelen=`echo "$var" |wc -L`
    ^^^ I'm assuming this is not a one liner...
    res=$(($sn-$linelen))
    
    echo -n "$var" > tmp1
    COUNTER=1
    
    while [ $COUNTER -lt $res ]
    do
        printf " " >> tmp1
        let COUNTER+=1
    done
    In all of that where is my "name"? When I ran the script all I got was 4 white spcaces the tmp1 file... I'm not quite sure how this is suppose to work.

    Thanks!

    Scorp

  4. #4
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Using what Cabhan has said here are some examples:

    ksearle@plyw9x214:~> var=kieren
    ksearle@plyw9x214:~> echo ${var:0:3}
    kie
    ksearle@plyw9x214:~> echo ${var:0:9}
    kieren

    Hope this helps
    Linux User #453176

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    With printf you can accomplish this with 1 line:

    Code:
    #!/bin/bash
    
    printf "%-5.5s\n" $1 >> tmp1

    Regards

Posting Permissions

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