Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: shell-script count to variable

  1. #1
    Join Date
    Jun 2011
    Location
    germany
    Beans
    6
    Distro
    Ubuntu 11.04 Natty Narwhal

    shell-script count to variable

    Code:
    #!/bin/bash
    
    TEST=$'count *.avi *.AVI *.mpeg *.MPEG *.divx *.DIVX *.mkv *.MKV *.mpg *.MPG *.mp4 *.MP4'
    
    echo $TEST
    why do i get a STRING result ( the echo command prints every filename )

    if i use the terminal command:

    Code:
    count *.avi *.AVI *.mpeg *.MPEG *.divx *.DIVX *.mkv *.MKV *.mpg *.MPG *.mp4 *.MP4
    i get an integer.

    I need an integer. Please help.

  2. #2
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: shell-script count to variable

    Use the $() form:
    Code:
    TEST=$(count *.avi *.AVI *.mpeg *.MPEG *.divx *.DIVX *.mkv *.MKV *.mpg *.MPG *.mp4 *.MP4)
    or, with backticks instead:
    Code:
    TEST=`count *.avi *.AVI *.mpeg *.MPEG *.divx *.DIVX *.mkv *.MKV *.mpg *.MPG *.mp4 *.MP4`
    See the "Command Substitution" section in the dash(1) or bash(1) man pages for more info.
    Last edited by gmargo; June 10th, 2011 at 04:29 PM.

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: shell-script count to variable

    as gmargo says, $( ...) or ` ... ` for running commands and capturing their output. $' ... ' is a different construct in bash.
    from man bash:
    Code:
           Words of the form $'string' are treated specially.  The word expands
           to string, with backslash-escaped characters replaced  as  specified
           by the ANSI C standard.  Backslash escape sequences, if present, are
           decoded as follows:
                  \a     alert (bell)
                  \b     backspace
                  \e
                  \E     an escape character
                  \f     form feed
                  \n     new line
                  \r     carriage return
                  \t     horizontal tab
                  \v     vertical tab
                  \\     backslash
                  \'     single quote
                  \"     double quote
                  \nnn   the eight-bit character whose value is the octal value
                         nnn (one to three digits)
                  \xHH   the eight-bit character whose value is the hexadecimal
                         value HH (one or two hex digits)
                  \cx    a control-x character

  4. #4
    Join Date
    Jun 2011
    Location
    germany
    Beans
    6
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: shell-script count to variable

    thanks for the answers.
    I tried both ways to solve my problem, but now i get:

    Code:
    /home/josanna/Documents/scripts/mv2d.sh: 17: count: not found
    but in the folder I run this script, are *.avi - files

    iam using:
    ubuntu 11.04 natty
    gnome terminal && fish

    is there an other way to count files from the same data type?

  5. #5
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: shell-script count to variable

    its not telling you that its not finding avi files, its telling you that it can't find the 'count' command

    I've never heard of a count command like this either actually. you say it works from the command line for you, what is the result of the commands:
    Code:
    type count
    and
    Code:
    which count
    personally i would count files like:
    Code:
    i=$(ls *.avi *.mpg *.etc | wc -l)

  6. #6
    Join Date
    Jun 2010
    Location
    Sacramento, CA
    Beans
    196
    Distro
    Xubuntu 11.04 Natty Narwhal

    Re: shell-script count to variable

    Remove the quotes? From what I understand, something such as
    Code:
    [ "$" -ne "1"]
    would be read as a string, while something like
    Code:
    [ $ != 1 ]
    would be read as an integer.
    Yeah?
    Last edited by GrantStoner; June 10th, 2011 at 08:36 PM.

  7. #7
    Join Date
    May 2011
    Beans
    273

    Re: shell-script count to variable

    Use 'wc' instead of count, I've never heard of it either.
    Your left hand is touching your face.

  8. #8
    Join Date
    Jun 2011
    Location
    germany
    Beans
    6
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: shell-script count to variable

    Thanks for your answers.
    First of all i think I have to apologize for my english

    @DaithiF i made a Screenshot:

    myTERMINAL

    here you can see the count - command [line 3+4], and what it should do [result printed in line 5]

    then i typed in the commands you asked.

    The command "mv2d" executed my shell script with:

    Code:
    i=$(ls *.avi *.mpg *.etc | wc -l)
    i could save an Integer Value in "i", but i got the errors[line 11+12]. Is there a way to hide these errors?

  9. #9
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: shell-script count to variable

    interesting. I think I understand now. you're running fish as your interactive shell. It has a builtin count command, the only shell I know that does.

    your script however has a shebang of #!/bin/bash, so gets run by bash rather than fish, and so the fish-specific count command is not available.

    Since i don't know much about fish I'm going to stick with bash -- to suppress the 'cannot access *.mpg' etc type warnings, set the nullglob option in your script with the line:

    Code:
    shopt -s nullglob
    anywhere before the line where you count the files.

  10. #10
    Join Date
    Jun 2011
    Location
    germany
    Beans
    6
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: shell-script count to variable

    thanks again for your fast reply...

    I take another screenshot...
    Terminal

    i still got an error, the command shopt is not guilty =(
    do I have to download an additional package?

Page 1 of 3 123 LastLast

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
  •