Shell programming

Discussion in 'Programming/Scripts' started by alleks, Feb 7, 2011.

  1. alleks

    alleks Member

    How can I loop a file and then check if contains a value?

    I have made this script
    Code:
    #!/bin/bash
    
    date=`date +%d-%m-%y`
    
    workdir=/var/backup/$date
    files=$workdir/files
    
            if [ $? -eq 0 ]; then
                    ls $workdir/*.tar.gz > $files
                    for file in $files
                            do
                                    echo `Processing $file file...`
                                    if [[ `$file` == `alex` ]]; then
                                            echo `yuhu`; echo
                                    fi
                            done
            fi
    
            if [ $? -eq 1 ]; then
                    echo `not found`
            fi
    
    
    And when I run it I get:
    Code:
    ./file-operations.sh: line 12: Processing: command not found
    
    ./file-operations.sh: line 13: /var/backup/07-02-11/files: Permission denied
    ./file-operations.sh: line 13: alex: command not found
    ./file-operations.sh: line 14: yuhu: command not found
    
    
    I need to check if $file in any line the value "alex" so then I can run my commands.

    Tnx
     
    Last edited: Feb 7, 2011
  2. alleks

    alleks Member

    Ok manage to do it this way, but still listening to suggestions

    Code:
    for file in $files
       do
          apache=`cat $file | grep apache`
          echo -e `$apache\n`
       done
    
    But still getting some problems
    1. Why do I always have to use ` and not " ?
    2. echo -e `$apache\n` outputs at the en of the result: Permission denied
     
    Last edited: Feb 8, 2011
  3. falko

    falko Super Moderator ISPConfig Developer

    Take a look here: http://www.dba-oracle.com/linux/quotes_in_shell_scripts.htm

    By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.
     
  4. alleks

    alleks Member

    Oh that issue was clarified after trying all kinda stuff.

    I have the script up and running (tomorrow I will post it here so you can have a look and share some suggestions)

    What I am looking now for is to loop variables, I mean:

    • Let's say I have 3 variables today to work with, and those variables contain all my data
    • What can I do so I won't have to call all the time $var1, $var2 and $var3 but only call a $var$i and $i to auto increment to the respective number?
     
  5. falko

    falko Super Moderator ISPConfig Developer

  6. alleks

    alleks Member

    Tnx falko but I have decided to do it another and I have run into another problem, but hopefully some1 can help me out (I'll be tryin to fix it to)

    Ok here it goes,

    Let's say I have 5 variables

    Code:
    var1=...
    var2=...
    ...
    var5=...
    And then I use each variable to run various parameters, for example:

    Code:
    [COLOR="red"]op1[/COLOR]=`cat $files | grep [COLOR="Red"]$var1[/COLOR]`
    [COLOR="red"]op2[/COLOR]=`cat $files | grep [COLOR="Red"]$var2[/COLOR]`
    ... you get the point
    
    What I have decided to do is to use a for, because I always now the number of my variables and I can control it, and I am using this way

    Code:
    for (( i = 1; i <= 6; i++ ))
    			do
    				op$i=`cat $files | grep $var$i`
    			done
    
    Everything ok till now, the problem comes when I extend the for to fit my needs:

    Code:
    for (( i = 1; i <= 6; i++ ))
    			do
    				op$i=`cat $files | grep $var$i`
    				[U][COLOR="Blue"]for op in $op$i[/COLOR][/U]
    					do
    					   ....
    					done
    			done
    
    The blue underlined line is the 1 that give me trouble, I want it to do a
    Code:
    for op in op1
    for op in op2
    ...
    
    but by using the $i variable. I know that op$i already contains op1, op2, op3, op4 and op5 but how can I access it by not doing $op$i?

    Hope you understand my question (any1) and also able to help me out!
     
  7. alleks

    alleks Member

    Ok, sorry folks. I know that for some of you sounds nobish my question, but I am still learning.

    But anyway, I've solved it. It was all about trying different things. Here is how I've solved it, for those that might encounter the same problem.

    First of all, I've changed the name of my variables from

    Code:
    var1=...
    var2=...
    ...
    
    to

    Code:
    var[1]=...
    var[2]=...
    ...
    
    then I just did this

    Code:
    var[1]=abc
    var[2]=xyz
    ...
                    for i in 1 2 3 4 5
                            do
                                    op[i]=`cat /etc/passwd | grep ${var[i]}`
                                    for op in ${op[i]}
                                            do
    
    Hope some1 else will find this useful to, and just so you know... as guide I've used this tutorial:

    http://tldp.org/LDP/abs/html/arrays.html

    Tnx Falko
     

Share This Page