• I have created a script to move some stuff without needing to write lots of parameters.


    Bash
    #!/bin/bash
    
    
    echo "Copying $1 to /storage/Repositorio/TVSHOW/"
    
    
    cp -R -v /storage/Torrent/Download/Seeding/$1 /storage/Repositorio/TVSHOW/
    
    
    echo "Command finished."


    The thing is, I'd like to make it loop while it have parameters. Something like:


    Code
    while (have paramaters) {
    	echo "Copying $# to /storage/Repositorio/TVSHOW/"
    	cp -R -v /storage/Torrent/Download/Seeding/$# /storage/Repositorio/TVSHOW/
    	$count++
    }
    echo "$count dir/files were processed"
    echo "Check the verbose output"
    }


    Can anyone here help me with that? I actually only need to know the "(have parameters)" part.

  • I came up with this, so I can have at most 10 dirs/files at a time.



    But that didn't work, I got this error:


  • [ is a command, not a bracket.
    In shell scripting, the if statement takes a numerical value as a parameter. Thus, You need to use another command to evaluate your if condition. Here comes the [ statement. [ is a command that take an expression as a parameters and return 0 if the expression is false or 1 if it is true.


    So, You should have done:

    Code
    if [ -n $1 ]


    By the way, you could have used a for and the shell variable $@, it would have been cleaner.


    Like:

    Code
    for dir in $@; do
         echo "Copying $dir to /storage/Repositorio/TVSHOW";
         cp -afv "/storage/Torrent/Download/Seeding/$dir" /storage/Repositorio/TVSHOW ;
    done


    Beware that the above code does not handle folders' names with spaces.


    You should read http://freeos.com/guides/lsst/.

  • Wouldn't the script copy to directories with spaces if the directory were in quotes like;

    Code
    for dir in $@; do
         echo "Copying $dir to /storage/Repositorio/TVSHOW";
         cp -afv "/storage/Torrent/Download/Seeding/$dir" /storage/Repositorio/"TV SHOW" ;
    done
  • No, the issue lies in the for statement and in $@ variable expansion.
    for iterates trough the values in in and uses the space as a separator.
    Thus, if $@ contains "my folder", for will iterate through "my" and then "folder".
    IMHO files and folders that have spaces in their names are evil. It's so easy to replace spaces with underscores...
    Anyway, if one is willing to take the dark side pathway, he can use a while and the shift command.
    Like that:

    Code
    while [ "$1" ];
    do
         echo "Copying $dir to /storage/Repositorio/TVSHOW";
         cp -afv "/storage/Torrent/Download/Seeding/$dir" /storage/Repositorio/TVSHOW;
         shift 1;
    done
  • chente

    Hat das Thema geschlossen.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!