Results 1 to 8 of 8

Thread: IFS scripting

  1. #1
    Join Date
    Dec 2010
    Beans
    29

    IFS scripting

    hi to all.

    I couldn't, for some reason, post an new thread in the "development" section so i posted it here. feel free to move it around

    I'm new to bash scripting but i try, mainly for educational purposes, to make a script for assigning the corrected name of a file (backslashes before spaces) to a variable so it can be fed to a command line argument.

    my code isn't anywhere near working!! so i would like a bit of help!

    Method No.1

    Code:
    IFS="
    "
    for space_file in $(find * -type f); do
    	cool_file=
    	IFS="\*" ### here i know i miss the correct escape char for "every char"
    
    	for char_check in "$space_file"; do
    #	echo character "$char_check" # test
    
    		if [ "$char_check" = " " ];  then 
    			char_check="\ "
    		fi 
    		cool_file=$(echo $cool_file$char_check)
    	done
    	echo $cool_file
    done
    
    unset IFS
    Method No.2

    Code:
    IFS="
    "
    for space_file in $(find * -type f); do
    	cool_file=
    	unset IFS
    	for char_check in "$space_file"; do
    		
    		cool_file="$cool_file\\$char_check"
    # if this worked i know that i will have to "tail" out the last char. 
    	done
    	echo  $cool_file
    done
    
    unset IFS
    So, is Method No.1 going to work if i set IFS to each and every character and whow do i do that?
    and, why Method No.2 just adds a "\" in th beginning of the file???

    please please help!
    g.
    Last edited by stamoulohta; January 19th, 2011 at 01:11 PM.

  2. #2
    Join Date
    May 2008
    Beans
    518

    Re: IFS scripting

    In method 2, do
    Code:
    for char_check in $space_file; do
    		cool_file="$cool_file\\ $char_check"
    instead of
    Code:
    for char_check in "$space_file"; do
    		cool_file="$cool_file\\$char_check"
    (remove the Quotes)
    It will still write the backslash at the beginning of the filename, however. That should be easily removed with sed.

    EDIT: dammit, what is it with this double-posting?! Is it because of the site?
    Last edited by dracayr; January 19th, 2011 at 03:30 PM.

  3. #3
    Join Date
    May 2008
    Beans
    518

    Re: IFS scripting

    In method 2, do
    Code:
    for char_check in $space_file; do
    		cool_file="$cool_file\\ $char_check"
    instead of
    Code:
    for char_check in "$space_file"; do
    		cool_file="$cool_file\\$char_check"
    (remove the Quotes)
    It will still write the backslash at the beginning of the filename, however. That should be easily removed with sed.
    Last edited by dracayr; January 19th, 2011 at 03:30 PM.

  4. #4
    Join Date
    May 2008
    Beans
    518

    Re: IFS scripting

    In method 2, do
    Code:
    for char_check in $space_file; do
    		cool_file="$cool_file\\ $char_check"
    instead of
    Code:
    for char_check in "$space_file"; do
    		cool_file="$cool_file\\$char_check"
    (remove the Quotes)
    It will still write the backslash at the beginning of the filename, however. That should be easily removed with sed.

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

  6. #6
    Join Date
    Dec 2010
    Beans
    29

    Re: IFS scripting

    Thank you both for your answers!!

    i got it! this is how it looks now:
    Code:
    #!/bin/bash
    
    IFS="
    "
    for space_file in $(find * -type f); do
    	cool_file=
    	unset IFS
    	for char_check in $space_file; do
    		
    		cool_file="$cool_file\\ $char_check"
    
    	done
    	echo  $cool_file | sed 's/\\//'
    done
    
    unset IFS
    
    exit
    I'll mark it as [SOLVED] so others can see but I would like (out of curiosity) Method No.1 corrected by someone if he could,

    and some help for making shed substitute all the occurrences and not just the first one.

    example:
    Code:
    echo "my name is george" | sed 's/ /\\ /'
    
    my\ name is george
    but i want
    Code:
    echo "my name is george" | sed '??????'
    
    my\ name\ is\ george
    Thank you all once more.

    PS: @dracayr, it is the sites problem... read an announcement somewhere.
    Last edited by stamoulohta; January 19th, 2011 at 06:11 PM.

  7. #7
    Join Date
    Dec 2010
    Beans
    29

    Re: IFS scripting

    Thank you both for your answers!!

    i got it! this is how it looks now:
    Code:
    #!/bin/bash
    
    IFS="
    "
    for space_file in $(find * -type f); do
    	cool_file=
    	unset IFS
    	for char_check in $space_file; do
    		
    		cool_file="$cool_file\\ $char_check"
    
    	done
    	echo  $cool_file | sed 's/\\//'
    done
    
    unset IFS
    
    exit
    I'll mark it as [SOLVED] so others can see but I would like (out of curiosity) Method No.1 corrected by someone if he could,

    and some help for making shed substitute all the occurrences and not just the first one.

    example:
    Code:
    echo "my name is george" | sed 's/ /\\ /'
    
    my\ name is george
    but i want
    Code:
    echo "my name is george" | sed '??????'
    
    my\ name\ is\ george
    Thank you all once more.

    PS: @dracayr, it is the sites problem... read an announcement somewhere.
    Last edited by stamoulohta; January 19th, 2011 at 07:01 PM.

  8. #8
    Join Date
    Dec 2010
    Beans
    29

    Re: IFS scripting

    Thank you both for your answers!!

    i got it! this is how it looks now:
    Code:
    #!/bin/bash
    
    IFS="
    "
    for space_file in $(find * -type f); do
    	cool_file=
    	unset IFS
    	for char_check in $space_file; do
    		
    		cool_file="$cool_file\\ $char_check"
    
    	done
    	echo  $cool_file | sed 's/\\//'
    done
    
    unset IFS
    
    exit
    I'll mark it as [SOLVED] so others can see but I would like (out of curiosity) Method No.1 corrected by someone if he could,

    and some help for making shed substitute all the occurrences and not just the first one.

    example:
    Code:
    echo "my name is george" | sed 's/ /\\ /'
    
    my\ name is george
    but i want
    Code:
    echo "my name is george" | sed '??????'
    
    my\ name\ is\ george
    Thank you all once more.

    PS: @dracayr, it is the sites problem... read an announcement somewhere.
    Last edited by stamoulohta; January 19th, 2011 at 06:07 PM.

Tags for this Thread

Bookmarks

Posting Permissions

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