Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: arguments in for loop

  1. #1
    Join Date
    Jan 2014
    Beans
    2

    Question arguments in for loop

    hy guys

    i'm having a little problem in a for loop i can't seem to solve. i want to count different types of videofiles (avi, mvk, mp4, etc) in a map and write the number of each type to the file "aantal".
    i'm going to change the writing to a file to something usefull later on but i figure i'd get this working first.
    So far i have this small code.

    Code:
    #!/bin/bash -xv
    
    cd /media/sf_Downloads/testmap
    for i in *.avi *.mkv *.mp4
    do 
     find -P "${i##*.}" | wc -l >>/home/cedric/aantal
    done
    i know the fault is somewhere in
    Code:
    "${i##*.}"
    but i don't know why or how it works
    This is what it get's me
    Code:
    + for i in '*.avi' mkv '*.mp4'
    + wc -l
    + find -P avi
    find: ‘avi’: Bestand of map bestaat niet
    + for i in '*.avi' mkv '*.mp4'
    + wc -l
    + find -P mkv
    find: ‘mkv’: Bestand of map bestaat niet
    + for i in '*.avi' mkv '*.mp4'
    + wc -l
    + find -P mp4
    find: ‘mp4’: Bestand of map bestaat niet
    I want it to find any avi-, mp4-, mkv-files regardless of the name (i.e. "Man Of Tai Chi (2013).mkv" or "Burn Notice - The Fall of Sam Axe (2011).mkv" ") but it doesn't do that for now.
    any help please?
    other methods of doing something like this are also welcome

    thanks in advance,

    Cedric

  2. #2
    Join Date
    Jul 2008
    Location
    The Left Coast of the USA
    Beans
    Hidden!
    Distro
    Kubuntu

    Re: arguments in for loop

    Moved to Programming Talk.
    Please read The Forum Rules and The Forum Posting Guidelines

    A thing discovered and kept to oneself must be discovered time and again by others. A thing discovered and shared with others need be discovered only the once.
    This universe is crazy. I'm going back to my own.

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: arguments in for loop

    Check out BashFAQ 004 (link in my signature).

  4. #4

    Re: arguments in for loop

    Try an array:

    Code:
    Source=(*.avi *.mkv *.mp4)
    for i in ${Source[@]} ; do <stuff here>  ; done
    Windows assumes the user is an idiot.
    Linux demands proof.

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: arguments in for loop

    Code:
    #!/bin/bash
    
    dir=.
    exts=( avi mkv mp4 mov )  # array of relevant extensions
    
    # associative array of counters, count[X] = number_of_X
    declare -A count
    
    # construct the regex matching desired file types
    printf -v rgx "%s|" "${exts[@]}"
    rgx=".*[.](${rgx%?})"
    
    # loop through the output of find supplied with the prepared regex
    while read -rd $'\0' f
    do
      ext=${f##*.}                         # get file extension
      ext=${ext,,}                         # normalize ext to lowercase
      count[$ext]=$(( ${count[$ext]}+1 ))  # add one to the counter
    done < <( find "$dir" -regextype posix-extended -iregex "$rgx" -type f -print0 )
    
    # print the key/value pairs present in the count array
    for k in ${!count[@]}; do echo "$k: ${count[$k]}"; done
    Code:
    $ cd test
    $ ls
    test2.avi  test2.mp4  test.mkv  test.mp4
    test2.MKV  test.AVI   test.Mov  videos.sh
    $ ./videos.sh 
    mkv: 2
    mp4: 2
    mov: 1
    avi: 2
    redirecting to file inside the script is not necessary. You can redirect the script itself wherever you want, which gives you way more flexibility
    Code:
    ./script
    ./script | some | commands
    ./script > file
    Last edited by Vaphell; January 8th, 2014 at 09:35 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: arguments in for loop

    Code:
    for ext in avi mp4 mkv
    do
              echo $ext:$(find . -name "*.$ext" | wc -l) >> /home/cedric/aantal
    done
    or am I missing something?
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: arguments in for loop

    Quote Originally Posted by ofnuts View Post
    Code:
    for ext in avi mp4 mkv
    do
              echo $ext:$(find . -name "*.$ext" | wc -l) >> /home/cedric/aantal
    done
    or am I missing something?
    Code:
    [sisco@acme tmp]$ mkdir foo
    [sisco@acme foo]$ cd foo
    [sisco@acme foo]$ > "this is an avi file.avi"
    [sisco@acme foo]$ > "this is another one.AVI"
    [sisco@acme foo]$ ls -al
    total 40
    drwxr-xr-x  2 sisco users  4096 Jan  9 11:29 .
    drwxr-xr-x 11 sisco users 36864 Jan  8 13:53 ..
    -rw-r--r--  1 sisco users     0 Jan  9 10:51 this is an avi file.avi
    -rw-r--r--  1 sisco users     0 Jan  9 10:51 this is another one.AVI
    [sisco@acme foo]$ find ./ -name "*.avi" | wc -l
    1
    [sisco@acme foo]$ > "and now for
    > something
    > completely
    > different.avi"
    
    [sisco@acme foo]$ ls -al
    total 40
    drwxr-xr-x  2 sisco users  4096 Jan  9 11:26 .
    drwxr-xr-x 11 sisco users 36864 Jan  8 13:53 ..
    -rw-r--r--  1 sisco users     0 Jan  9 11:26 and now for?something?completely?different.avi
    -rw-r--r--  1 sisco users     0 Jan  9 10:51 this is an avi file.avi
    -rw-r--r--  1 sisco users     0 Jan  9 10:51 this is another one.AVI
    [sisco@acme foo]$ find ./ -name "*.avi" | wc -l
    5

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: arguments in for loop

    Code:
    for ext in avi mp4 mkv
    do
              echo $ext:$(find . -iname "*.$ext" -print0 | tr -d '\n' | tr '\0' '\n' | wc -l) >> /home/cedric/aantal
    done
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: arguments in for loop

    names are not important so it's better to replace them with something predictable to avoid the whitespace issues. You can simply use something like -printf "x" | wc -c
    Code:
    for ext in avi mp4 mkv
    do
        echo $ext:$( find . -iname "*.$ext" -printf "x" | wc -c )
    done > /home/cedric/aantal
    that said, running find n times is a bit wasteful. I tried to do this in one go, by creating universal regex for find and then counting extensions
    Last edited by Vaphell; January 9th, 2014 at 01:19 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Jan 2014
    Beans
    2

    Re: arguments in for loop

    hy guys

    thanks a lot with these examples. i will definitly try them out and try to understand how they work.

    kind regards

    Cedric

Page 1 of 2 12 LastLast

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
  •