LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash 2 several words filename (http://www.linuxquestions.org/questions/programming-9/bash-2-several-words-filename-915753/)

trintukaz 11-27-2011 04:16 AM

Bash 2 several words filename
 
Hello guys. I am making a script which one of the functions hide files.
Code:

#!/bin/bash

if [ $1 == "--hide" ]
then

        if [ $2 != "--all" ]
        then
                files=`echo $@ | awk '{$1=""; print $0}'`
                for i in $files
                do
                        if [ -e $i ]
                        then
                                echo "$i -> .$i"
                                mv $i .$i
                        fi
                done
        fi
fi

Here is my code but I face with a problem. When filename has few words hidding does not working.

sinuhe 11-27-2011 05:18 AM

.hidden files
 
First, you would do better with a case statement instead of if statements. Second, do you mean that when you have spaces in a file name, that your script fails? If so, double quote your expansions, e.g. "$i"

trintukaz 11-27-2011 05:37 AM

i tried double quote before but still dos not work.

trintukaz 11-27-2011 06:33 AM

maybe any suggestions?

grail 11-27-2011 09:58 AM

If file names have spaces in them the for loop will not work as it will break each file into the whitespace separated pieces.

Maybe you could show us how you are calling your script as the format you currently have is a little vague?

NevemTeve 11-27-2011 11:00 AM

Use find(1).
something like this (not tested):

Code:

find . -name "*mask" -exec mv "{};" ."{};"

David the H. 11-27-2011 11:07 AM

Yes, more info please. But to start with, check out these links:

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

http://mywiki.wooledge.org/BashPitfalls
See 1, 2, 4, 10 in particular. And perhaps 24 and 37.

$(..) is highly recommended over `..`


Have a look at the shift built-in command.

Arrays might possibly be of use too, depending on your exact requirements. See the BashGuide for more on them and other general scripting advice:

http://mywiki.wooledge.org/BashGuide

trintukaz 11-27-2011 01:02 PM

i call my script like this
Quote:

~/ScriptDestination --hide filename

grail 11-27-2011 09:12 PM

Well if you are guaranteeing all file names look just like that, I see no issues as script will work without issue.


All times are GMT -5. The time now is 06:58 PM.