FedoraForum.org - Fedora Support Forums and Community
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    884
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Argument list with bash script

    I want to loop through the arguments in a bash script (ie if I call
    ./MyScript *.txt
    in a folder with files: a.txt b.txt c.txt
    I want to operate on each of those files.)

    I know the arguments are $1, $2, etc - but how do you construct a variable name like that - ie

    Code:
    #!/bin/bash
    echo number of arguments is $#
    
    for i in `seq 1 $#`; do
        echo argument ${i}: `$i` #this last part is the problem
    done;
    See what I'm trying to do? Is this how this should be approached?

    Thanks,
    Dave

  2. #2
    Join Date
    Oct 2007
    Posts
    884
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Comment:
    I know about
    Code:
    for i in $*; do
    echo $i
    done;
    But then I don't have the argument's index.

    Dave

  3. #3
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    10,278
    Mentioned
    129 Post(s)
    Tagged
    0 Thread(s)
    You could do this:
    Code:
    #!/bin/bash
    args=("$@")
    for ((i=0; i < $#; i++)) {
       echo "argument $((i+1)): ${args[$i]}"
    }
    OS: Fedora 44 x86_64 | DE/WM: Window Maker (X11) | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 (4)@3.30GHz | RAM: 32GB PC3-12800 DDR3 | Disks: Seagate Barracuda ST500DM002 500GB SATA, Seagate Constellation ES.3 ST1000NM0033 1TB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Intel 82579LM

  4. #4
    Join Date
    Oct 2007
    Posts
    884
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good idea.

  5. #5
    Join Date
    Oct 2004
    Location
    London, UK
    Posts
    4,991
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a couple of subtle but important bits in RupertPupkin's very nice and compact script

    args=("$@") is superior to the sometimes seen args=($@) since the former will not create separate array elements if any of the args have spaces in them (so actually this is a pretty cool way to iterate through a directory with filenames containing spaces)

    Note that the array index starts at zero, but the command line args start at one, which is why the test in the loop is 'i < $#' not 'i <= $#' and the argument index is offset by 1 in the output.

  6. #6
    Join Date
    Oct 2007
    Posts
    884
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Who would ever put spaces in file names!!?? That should be punishable by death

  7. #7
    Join Date
    Oct 2004
    Location
    London, UK
    Posts
    4,991
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know you're joking, but most media/music directories are full of spaced filenames.

  8. #8
    scottro's Avatar
    scottro is offline Retired Community Manager -- Banned from Texas by popular demand.
    Join Date
    Sep 2007
    Location
    NYC
    Posts
    8,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Spaces in filenames are quite frequent--not only in media files, but whenever users name files. Microsoft and Apple both accept spaces and have no trouble handling them, so users will make use of that.

    In my past job, though, I remember an incident where there were several seemingly identically named files. It turned out that a user had at times, written the file name and hit space rather than enter, so there were files like image.jpg and image.jpg with a space after the jpg.

    (Eventually, I ran cat -e on the directory, which puts a $ at the end of each line, so I could see that there were several like image.jpg$ and image.jpg $.)

    Point being, especially if you're working professionally, that whether it's a bad idea or not, you may often run into it.
    --
    http://srobb.net

Similar Threads

  1. help with a bash script
    By dan sawyer in forum Using Fedora
    Replies: 5
    Last Post: 6th July 2009, 04:07 PM
  2. yum / rpm with a list of packages as an argument???
    By pepe123 in forum Using Fedora
    Replies: 2
    Last Post: 19th December 2007, 01:17 PM
  3. Convert bash script to perl script
    By homey in forum Programming & Packaging
    Replies: 1
    Last Post: 2nd September 2006, 04:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •