Find the answer to your Linux question:
Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By watael
  • 1 Post By atreyu
I have a variable in my bash script and i want to run my script for multiple variables. Meaning not only one time, but cycle it for each of the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User postcd's Avatar
    Join Date
    Apr 2011
    Posts
    350

    How to run bash script several times for different inputs/variables


    I have a variable in my bash script and i want to run my script for multiple variables.


    Meaning not only one time, but cycle it for each of the inputs i have (i have list of around 20 domains i want to use as variables).


    I want to ask for an example on how to achieve this or linking me to article or saying what to input into Google, my english is bad and i cant find anything in Google related to my question.

    Thank You

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    you can hard code the 20 domains as variables right in your script or

    put them in a separate file that gets read by your script.

    here's an example of the 2nd way. first, your text file might look like:
    Code:
    www.domain1.com
    www.domain2.com
    www.domain3.com
    then your bash script could look like:

    Code:
    #!/bin/bash
    [ $# -ne 1 ] && echo "give me a text file w/domains" && exit 1
    file=$1
    [ ! -f "$file" ] && echo "$file not found" && exit 1
    
    while read domain; do
      echo "Doing something with domain $domain ... "
    done < <(cat $file)
    The variable $domain will be populated with each of the domain names listed in your text file.

    (code untested)

  3. #3
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    uuoc!
    Code:
    while read var; do :do stuff with "$var"; done < file
    postcd likes this.

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by watael View Post
    uuoc!
    i strike again!

  6. #5
    Linux User postcd's Avatar
    Join Date
    Apr 2011
    Posts
    350
    Quote Originally Posted by atreyu View Post
    Code:
    #!/bin/bash
    [ $# -ne 1 ] && echo "give me a text file w/domains" && exit 1
    file=$1
    [ ! -f "$file" ] && echo "$file not found" && exit 1
    
    while read domain; do
      echo "Doing something with domain $domain ... "
    done < <(cat $file)
    your script asks for file, but exits after that, as a noob im unsure where to input the path to file with domains?
    also any way to add the domains to script itself, not needing to read from file?

  7. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by postcd View Post
    your script asks for file, but exits after that, as a noob im unsure where to input the path to file with domains?
    sorry, i did not make that clear. just put the text file in the same directory as the script, then when u call your script, include the text file as the first argument after the script, e.g.:

    Code:
    ./script.sh domain-list.txt
    if the text file is not in the same dir, just specify the path to it, e.g.:
    Code:
    ./script.sh /home/user/Desktop/domain-list.txt
    or whatever.

    also any way to add the domains to script itself, not needing to read from file?
    sure, that is easy. but tedious. i find it easier to update/edit a list, rather than modify a script, but here's how you can do it. at the top of the script, create a variable that contains all domain names, space-separated, e.g.:

    Code:
    #!/bin/bash
    
    # this is the variable that will contain the list of domain names
    domains='ww.domain1.com  www.domain2.com  www.domain3.net'
    
    # now loop thru all the domains
    for domain in $domains; do
      echo "doing something with $domain ... "
    done
    postcd likes this.

  8. #7
    Linux User postcd's Avatar
    Join Date
    Apr 2011
    Posts
    350
    Quote Originally Posted by atreyu View Post
    # this is the variable that will contain the list of domain names
    domains='ww.domain1.com www.domain2.com Enter my Domain | The info demense'
    Please how can i add into this variable phrasses with spaces ?

    like:

    domains='some phrasse some other prasse third phrasse //> '

    ?

    or im forced to use external file, or other way?

  9. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    use a bash array, e.g.:

    Code:
    declare -a domains
    domains[0]="the first thing"
    domains[1]="this is 2nd"
    domains[2]="and so on"
    
    #for domain in ${domains[*]}; do
    for (( i=0;i<${#domains[*]};i++ )); do
      domain=${domains[$i]}
    ...
    done
    Last edited by atreyu; 11-25-2013 at 10:32 PM. Reason: fixed "domain in $domains" loop

Posting Permissions

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