1

hi guys i have a problem with this code:

for dir in ~/Documents/CMDsh/*/     # list directories in the form "/tmp/dirname/"
do
    countFolder=$((countFolder+1));
    #echo $dir; #res: /home/arutosio/Documents/CMDsh/20060 Little Non - Hanamaru Sensation (TV Size)/
    dir=${dir%*/};#remove the trailing "/" #res: /home/arutosio/Documents/CMDsh/20060 Little Non - Hanamaru Sensation (TV Size)
    nameFolder=${dir##*/}; #print everything after the final "/" #res: 20060 Little Non - Hanamaru Sensation (TV Size)
    pathNameFolder="$(echo $nameFolder | sed 's/ /\\ /g')"; #res: 20060\ Little\ Non\ -\ Hanamaru\ Sensation\ (TV\ Size)
    pathNameFolder="$(echo $pathNameFolder | sed 's/(/\\(/g')"; #res: 20060\ Little\ Non\ -\ Hanamaru\ Sensation\ \(TV\ Size)
    pathNameFolder="$(echo $pathNameFolder | sed 's/)/\\)/g')"; #res: 20060\ Little\ Non\ -\ Hanamaru\ Sensation\ \(TV\ Size\)
    echo "NumFolder: $countFolder  Creating... \"$nameFolder.osz\"";    # print everything after the final "/"
    echo "zip -r -j -9 ~/osuLazerBeatmap/$pathNameFolder.zip $pathNameFolder/*"; #res: >
    #zip -r -j -9 ~/osuLazerBeatmap/20060\ Little\ Non\ -\ Hanamaru\ Sensation\ \(TV\ Size\).zip 20060\ Little\ Non\ -\ Hanamaru\ Sensation\ \(TV\ Size\)/*
    zip -r -j -9 ~/osuLazerBeatmap/$pathNameFolder.zip $pathNameFolder/*;
    echo '------------------------------';
done

but when i run my sh this line:

zip -r -j -9 ~/osuLazerBeatmap/$pathNameFolder.zip $pathNameFolder/*;

make this error: zip error: Invalid command arguments (short option '\' not supported) i tried to run a the result of this line in my terminal and it is working: zip -r -j -9 ~/osuLazerBeatmap/20060\ Little\ Non\ -\ Hanamaru\ Sensation\ (TV\ Size).zip 20060\ Little\ Non\ -\ Hanamaru\ Sensation\ (TV\ Size)/*

3

Try:

for dir in ~/Documents/CMDsh/*/
do
    countFolder=$((countFolder+1))
    dir=${dir%*/}
    nameFolder=${dir##*/}
    zip -r -j -9 ~/osuLazerBeatmap/"$nameFolder".zip "$dir"/*
    echo '------------------------------';
done

Notes:

  1. Always put references to shell variables in double-quotes. In this case, that meant replacing:

    zip -r -j -9 ~/osuLazerBeatmap/$nameFolder.zip $dir/*
    

    with

    zip -r -j -9 ~/osuLazerBeatmap/"$nameFolder".zip "$dir"/*
    

    This eliminates the need to attempt escaping with those three lines of sed code.

    The only exception to this rule is when you explicitly want word splitting or pathname expansion.

  2. An example may help. Let's consider a simple example with a directory with one file:

    $ ls
    Sensation (TV Size)
    

    Let's create a shell variable:

    $ f='Sensation (TV Size)'
    

    Now, let's try using the shell variable unquoted:

    $ ls $f
    ls: cannot access 'Sensation': No such file or directory
    ls: cannot access '(TV': No such file or directory
    ls: cannot access 'Size)': No such file or directory
    

    Notice how much better it works when the shell variable is quoted:

    $ ls "$f"
    Sensation (TV Size)
    

    By quoting the shell variable, no escaping is needed.

  3. The shell treats the end of the line as the end of the command. Thus, while semicolons at the end of a line don't hurt, they are unnecessary.

  • 1
    nice amazing thank you dude – Stefano Aruta Jul 31 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.