#1  
Old 7th February 2011, 13:59
alleks alleks is offline
Senior Member
 
Join Date: Nov 2010
Posts: 157
Thanks: 9
Thanked 9 Times in 9 Posts
Exclamation Shell programming

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 by alleks; 7th February 2011 at 14:09.
Reply With Quote
Sponsored Links
  #2  
Old 7th February 2011, 14:17
alleks alleks is offline
Senior Member
 
Join Date: Nov 2010
Posts: 157
Thanks: 9
Thanked 9 Times in 9 Posts
Default

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 by alleks; 8th February 2011 at 09:53.
Reply With Quote
  #3  
Old 8th February 2011, 20:05
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,741 Times in 2,575 Posts
Default

Quote:
Originally Posted by alleks View Post
[*]Why do I always have to use ` and not " ?
Take a look here: http://www.dba-oracle.com/linux/quot...ll_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.
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #4  
Old 8th February 2011, 20:13
alleks alleks is offline
Senior Member
 
Join Date: Nov 2010
Posts: 157
Thanks: 9
Thanked 9 Times in 9 Posts
Default

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?
Reply With Quote
  #5  
Old 9th February 2011, 16:57
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 41,701
Thanks: 1,900
Thanked 2,741 Times in 2,575 Posts
Default

Take a look at http://tldp.org/LDP/abs/html/
especially http://tldp.org/LDP/abs/html/arrays.html
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
The Following User Says Thank You to falko For This Useful Post:
alleks (11th February 2011)
  #6  
Old 11th February 2011, 12:28
alleks alleks is offline
Senior Member
 
Join Date: Nov 2010
Posts: 157
Thanks: 9
Thanked 9 Times in 9 Posts
Exclamation

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:
op1=`cat $files | grep $var1`
op2=`cat $files | grep $var2`
... 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`
				for op in $op$i
					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!
Reply With Quote
  #7  
Old 14th February 2011, 11:03
alleks alleks is offline
Senior Member
 
Join Date: Nov 2010
Posts: 157
Thanks: 9
Thanked 9 Times in 9 Posts
 
Default

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
Reply With Quote
The Following User Says Thank You to alleks For This Useful Post:
falko (15th February 2011)
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
is shell user allowed to sudo ? nsansari General 4 20th January 2012 16:45
How the shell user can access to other domain? cyberjames General 4 12th August 2010 19:15
Bashisms in shell scripts jwarnier Developers' Forum 2 6th August 2010 17:17
ISPC 2.2.9: Invalid user shell on Ubuntu/Debian martinfst Developers' Forum 4 13th January 2007 15:28
Shell nveid General 5 27th March 2006 14:49


All times are GMT +2. The time now is 23:56.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.