Script to read config file
hello,
The config file format which i designed for my script is, *PROJECTNAME : PATH1(TO SOME SUB FOLDER) : PATH2(TO SOME OTHER SUB FOLDER). PROJECTNAME will be given by user to the script. I am able to read the config file using basic shell commands like grep, cut. But want to read it using sed or awk as no. of lines in code which i have written is more. Also suggestion for config file format is requested. Thanks in advance Akshay |
I do not understand the question. Please show an example of how you want to process the file. Depending on what you want to do, there are many options available.
|
hello,
it is, config file format: proj1 : /abc/def/ijk : path/to/folder_1 proj2 : /efg/lmn/pqr : path/to/folder_2 proj3 : /xyz/ijk/mno : path/to/folder_3 . . . . The user will give the name of the project (i.e proj1/proj2/proj3) and the script should accordingly assign the corresponding paths to the variables which would be used for further processing. Hope it is enough. I want to know how to do this reading of script and assignment of path to some variables by using sed /awk/perl |
show us the script you have so far.
|
following is the configuration file contents
#*PROJECTNAME : PATH TO THE FILE : PATH TO THE FOLDER *proj1 : path1 for proj1 : PATH2 for proj1 *proj2 : path1 for proj2 : path2 for proj2 *proj3 : path1 for proj3 : PATH2 for proj3 *proj4 : PATH1 for proj4 : PATH2 for proj4 *proj5 : PATH1 for proj5 : PATH2 for proj5 *proj6 : PATH 1 for proj6 : PATH2 for proj6 the code which I have written is as follows, read -p "ENTER PROJECT NAME" nam CONFILE=sample_1.config cnt=`grep -ic *$nam $CONFILE` PRO=`grep -iw *$nam $CONFILE` if [ $cnt -gt 0 ] then echo $PRO > CONFILE_0 pth_0=`cut -d ":" -f 2 CONFILE_0` pth_1=`cut -d ":" -f 3 CONFILE_0` else echo "PROJECT NAME DOES NOT EXISTS IN CONFIG FILE" exit fi echo $pth_0 echo $pth_1 |
i have included the script into my .bashrc the script is running but it gives an error "sample_1.config: No such File or Directory". I have kept the config file along with the script.
Thanks in advance |
It is helpful to use CODE tags when posting code. The easiest way is to use Advanced editing and use the # button.
If sample_1.config exists but the script does not find it then the script's current working directory is not the one containing sample_1.config. The solution is either for the script to cd to the file's directory or to give the full path of the file. |
Thank you!!! I will take care of it next time.
Can you suggest any easy way to read the config file. using sed or awk. Also i am working on CRC generation using cksum command in linux, but want an alternate for it. |
Quote:
Have you got your script working? Best leave the checksum discussion in your other thread. |
yes Catkin my script is working properly.
I have used grep and cut commands for reading the config file. I thought awk & sed scripts won't be lengthy ... Also i want some improvements/suggestions for the code(already posted) Thanks for your response. |
Code:
read -p "ENTER PROJECT NAME" nam # this can be also implemented in awk |
Assuming the proj<n> string normally appears once and only once in the config file, I would code something like this. Follows convention of using upper case variable names only for bash specials and environment variables:
Code:
#!/bin/bash |
Thank you catkin for your code!!!
i will work on it and then inform accordingly if any thing goes wrong. |
|
All times are GMT -5. The time now is 02:43 PM. |