How to execute command present config file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to execute command present config file?
# 1  
Old 07-21-2016
How to execute command present config file?

I have written the following shell script
Code:
#!/bin/bash
file="/home/CSV/data.csv"
badfile="/home/CSV/bad/"
while IFS= read -r line
do
num_fields=`echo "$line" | awk -F'|' '{print NF}'`
field1=`echo "$line" | awk -F'|' '{print $1}'`
	echo $num_fields
	echo $field1
done <"$file"

the code is checking number of fields present and the value of first field. the number of fields can vary in data.csv. I wwant to move these variable and commands into one config file. some thing like this:

Config file:
Code:
file="/home/CSV/data.csv"
badfile="/home/CSV/bad/"
num_fields=`echo "$line" | awk -F'|' '{print NF}'`
field1=`echo "$line" | awk -F'|' '{print $1}'`

Code:
#!/bin/bash
. ./config.cfg
while IFS= read -r line
do
	echo $num_fields
	echo $field1
done <"$file"

by doing this I am able to get the value of variable but not output of unix commands

Last edited by rbatte1; 07-21-2016 at 07:51 AM.. Reason: Added CODE tags for config file
# 2  
Old 07-21-2016
The sourcing of the config file with . ./config.cfg will read them in as you suspect, but it actually executes them so the values set will be for that one instance. If $line has no value at that point, then that is what you are asking it to process. Once only.

Perhaps you would be better to consider another way like this:-
Code:
declare -a my_array
while read line
do
   my_array=($line)
   echo "number of fields ${#my_array[@]}"
   echo "First field ${my_array[0]}"
done < $file

Would that suit your purpose okay?


I hope that this helps and saves the performance costs of repetitive awk calls.

You could also use awk to process the files as a whole:-
Code:
awk '{print "Field one is \""$1 "\" with field count " NF}' $file

...but I suppose it depends what you want to do with the values after this.


Robin
# 3  
Old 07-21-2016
I'm not sure how to assess your code snippet. num_fields and field1 will hold meaningful values only if file has one single line only. Why, then, the while loop? With it, the two variables will hold the last line's values.

Please explain what exactly you want the config file to do.
# 4  
Old 07-22-2016
It is not a bit strange to assume external resources in the config file.
E.g. use $line, hoping that $line exists in the calling script.
If you really want that, you should put the per-line code in a function
Code:
file="/home/CSV/data.csv"file="bad"
badfile="/home/CSV/bad/"
runcode(){
num_fields=`echo "$line" | awk -F'|' '{print NF}'`
field1=`echo "$line" | awk -F'|' '{print $1}'`
}

and
Code:
. ./config.cfg
while IFS= read -r line
do
        runcode
        echo $num_fields
        echo "$field1"
done <"$file"

---------- Post updated at 14:02 ---------- Previous update was at 13:26 ----------

@rbatte1, the shell split differs from awk: a last empty field is not counted, $num_fields differs then.
Further, your code needs the delimiter
Code:
   IFS="|" my_array=($line)

and protection from argument expansion with
Code:
set -f

If the positional parameters can be used then there is no need for an array, and this works even with old Bourne shells
Code:
set -f
oIFS=$IFS; IFS="|"
while read -r line
do
   set -- $line
   echo "number of fields $#"
   echo "First field $1"
done <"$file"
set +f
IFS=$oIFS

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

2. Solaris

how to execute shell script present in unix machine remotely from windows

how to execute shell script present in unix machine remotely from windows? I having a shell script in my unix machine, need to execute the script remotely from my windows machine using Visual Basic or VBA macros. Thanks In Advance. --Suresh (1 Reply)
Discussion started by: sureshmani
1 Replies

3. Shell Programming and Scripting

how to execute a program present on another server using SFTP in perl

Hi, I want to execute a program which is present on another server. i want to use SFTP in perl, is it possible? how ? thanks. (1 Reply)
Discussion started by: anandgodse
1 Replies

4. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

5. Shell Programming and Scripting

Take the value of variable from a file and execute a command for each value

Hello Experts, I would like to know the best way to assign a value to variable from a given file and execute a command including this variable for each entry from the file. to be more clear, i have a file with different lines (each line with a different value). i want to substitute the variable... (2 Replies)
Discussion started by: Dendany83
2 Replies

6. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

7. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

8. Shell Programming and Scripting

sed command to parse Apache config file

Hi there, am trying to parse an Apache 'server' config file. A snippet of the config file is shown below: ..... ProxyPassReverse /foo http://foo.example.com/bar ..... ..... RewriteRule ^/(.*) http://www.example.com/$1 RewriteRule /redirect https://www.example1.com/$1 ........ (7 Replies)
Discussion started by: jy2k7ca
7 Replies

9. Programming

Assign a command to execute a file

Hi all, I want to assign a command name to a file.e.g. suppose I have a .sh file "xyz.sh". I want to execute the file by typing in "abc". The desired output is: $ abc should execute the "xyz.sh" file. Kind Regards, Qasim (4 Replies)
Discussion started by: qasim
4 Replies

10. HP-UX

How to execute a remote file with local command

Hello, I know this is somewhat strange, but please let me know if possible. I want to execute a program file in the remote machine with command on the local machine. Let me make things more clear. Suppose I have a cc on my local system and do not have that on the remote system. I want to use... (2 Replies)
Discussion started by: Veera_Raghav
2 Replies
Login or Register to Ask a Question