How to read a text file using a bash script
Hi everyone,
I know the logic of how I want to do it, but I really don't know how to implement this in bash. I hope someone is willing to help me figure this out. In a www sub directory of Apache called 'testing', I have made a PHP script that creates a file that has the users' IP-address as its file name. Eg. 192.168.1.239 There is also another file that I've called 'users' that contains following info: Code:
ip:name:directory Code:
192.168.1.239:jeroen:/jeroen/backup The bash script should check for files that have an IP-address as their name and match that address with the name that belongs to that ip-address (here is where the 'users' file comes into play). The best way is that the script creates an array 'arr' which contains: Code:
arr[0] = 192.168.1.239 I can't figure out how to do these relatively simple operations in bash. I'd be much obliged if you could point this out. Cheers, Jeroen |
Which way around do you want to do it? Start by looking for "files that have an IP-address as their name" and then look for a corresponding entry in the "users" file, or process the "users" file line by line looking for files with the given IP address? The second is easier but will not find cases of files named by IP address that do not have corresponding entries in the "users" file -- and vice versa, of course.
|
Quote:
1. Get the IPs: Code:
$ ls -1 (path to location of IP-named files) | while read ip 2. Read a file and filter by user IP: Code:
user_data=$(grep $ip < user-file) |
Quote:
Code:
find /directory_where_ip_files_are/ -name 192.168.1.236 So basically, I would process the user's file. @lutusp, I appreciate the hints:). And nope it's not homework. I've made a PHP page which allows my family to view the size of their backup archive, date when the last backup was made and whether the backup was successful or not. The backup script is running as a cron job: there is a script in every users backup directory with the correct parameters to invoke the script. Now, I have included a 'backup now' button which drops a file with their IP in the aforementioned folder. The 'users' file is my idea to match things up so that I can invoke the script with the correct parameters. I knew enough PHP to get this done (I'm not very brilliant when it comes to programming)but when it comes to bash:scratch: |
The read command uses the value of IFS as its delimiter, so you can do this:
Code:
#!/bin/bash You can read into an array with read -a arr, but I don't see any benefit in this case. |
Hi ntubski,
I coughed up this after a while Code:
#line="192.168.1.254:Jeff:dir" Now I can use your file finding snippet and I'm one step closer. To give the bigger picture: - Deltacopy is running on the Windows computers - A script with root privileges is (via cron) invoked to start the backup. The script chowns the users' backup dir to make sure that after the copy it has the correct user permissions. Now, in the scenario where a user can invoke the backup with a PHP page where there is a 'backup now' button: - When the user clicks on 'back up now' button, a file with the user ip will be created in the same dir as where the PHP script is. - Root script will find out whether there are ip files and then invokes a - user script which will make the backup (so it will call the Deltacopy server), chowns the files in the backup so the user has permission and then deletes the ip file (the root script will have set the correct permission so that the user script can delete this file). In order for the user script to work, it needs the ip address and backup dir (which can be found in the 'users.txt' file). I want to pass these arguments in an array from the root script to the user script. That explains my use of the array. |
okay I got some more:)
Code:
# First read the "users.txt" file into an array called "data". The split part refuses I'm afraid. How do I split on : and put it in the variable ip?. At the moment it just strips the delimeter but doesn't put the first part into the variable ip... Code:
# Next split each record of data to obtain the ip address. |
Quote:
Code:
ip="${data[0]}" |
Quote:
Code:
php -e /path/to/your/script Code:
#!/bin/php Code:
arr[1]=$(cut -d : -f 0) cheers Zhjim |
All times are GMT -5. The time now is 05:32 AM. |