Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
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.
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.
sed or awk could be used to read the config file and make values from it available to the script but I think grep would be easier. Is there any reason for preferring sed or awk?
Have you got your script working?
Best leave the checksum discussion in your other thread.
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
conf_file=sample_1.config
# Error trap. Could also test whether it is an ordinary file
if [[ ! -r "$conf_file" ]]; then
echo "ERROR: $conf_file not readable or does not exist"
exit 1
fi
read -p "ENTER PROJECT NAME: " proj_nam
oIFS=$IFS # Record value for restoring later
n_matches=0
while read -r line
do
if [[ $line = "" ]]; then
echo "ERROR: $proj_nam not found in $conf_file"
exit 1
fi
((n_matches++))
IFS=: # For splitting $line at : characters
array=( $line )
path1=${array[1]} # May want to strip leading and trailing spaces too
path2=${array[2]}
done <<< $( grep "*$proj_nam :" "$conf_file" )
case $n_matches in
1 )
echo "DEBUG: path1: '$path1', path2: '$path2'"
;;
* )
echo "ERROR: $proj_nam found $n_matches times in $conf_file"
exit 1
esac
IFS=$oIFS # Restore original value
# Here do whatever you want with those paths
exit 0
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.