Find the answer to your Linux question:
Results 1 to 4 of 4
I'm trying to create a for loop that uses a function to display the directories in my script. questions can I use variables in this or do i have to ...
  1. #1
    Just Joined!
    Join Date
    Jun 2007
    Posts
    3

    help with for loops and functions

    I'm trying to create a for loop that uses a function to display the directories in my script. questions can I use variables in this or do i have to display each directory in my loop. I'm using bash here's an example


    #!/bin/bash
    lsl () { ls -l ; }
    for t in /proc/ /home/bdrumright/ /var/log/ /etc/ /bin/
    do
    lsl $t
    done


    output only displays the first directory /proc/ five times in succession, which i thought it would display all 5 directories individually

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Welcome to the forum.

    Replace:

    Code:
    lsl () { ls -l ; }
    with:

    Code:
    lsl () { ls -l $1 }
    Regards

  3. #3
    Just Joined!
    Join Date
    Jun 2007
    Posts
    3

    thanks

    I appreciate the help. It looks like this now and works!
    #!/bin/bash
    lsl () { ls -l $1 ; }
    for t in /proc/ /home/bdrumright/ /var/log/ /etc/ /bin/
    do
    lsl
    done

    I'll probably be asking alot of questions here, I am a student and I'm hoping for a career in linux. If there is any other places or books I can use to help me plz share..... Thanks again

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You're welcome. Here you have some useful links:

    LinuxCommand.org: Learning the shell.
    http://www.tldp.org/LDP/Bash-Beginne...tml/index.html
    http://tldp.org/LDP/abs/html

    Also to read the posts here, try to solve the problems without seeing the solution, and compare your results with other results. Try to help others here, it's a great challenge and helpful to learn Linux.

    Regards

Posting Permissions

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