Reading selected lines from a param file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading selected lines from a param file
# 1  
Old 04-15-2012
Reading selected lines from a param file

Hi all,

I have a question for the Gurus. I apologize if this has bee shared before but I couldn't find the link.

I am trying to read parameters from an external parameter file. What I m trying to achieve is read selected lines from an external parameter file into the script. for eg my param file is going to look like


Code:
Please feed the user information below
"abc"
Please feed the password information below
"password"
Please feed the table information below
"table"

Enclosed I quotes is the variable which User want to pass over and would be dynamically changed. I want to keep this file protected and call from a script as

Script.ksh has
Code:
User=$1
Pwd=$2
Table=$3

How do I pass the 2nd 4th and 6th line into the script from an external file. Can I create the .ini file to achieve it. Please let me know. Quick response is much appreciated.

Best regards..

Last edited by Scrutinizer; 04-16-2012 at 01:58 AM.. Reason: code tags
# 2  
Old 04-16-2012
"Keep this file protected" is the key bit of information here.

I assume you wish the contents of your parameter file to not be visiable to those that have execute permissions on the script - Right?

The best way to do this is using the sudo tool, is it installed on your system?

If it is, do you have permission to make changes to the /etc/sudoers, or can you get changes done by the sysadmin on your behalf?
# 3  
Old 04-16-2012
If your param file is hardcoded, I mean if you always have the values on 2nd, 4th and 6th line you can use

1. If you want to keep the quotes
Code:
 sed -ne '2 p' -e '4 p' -e '6 p' paramfilename

2. If quotes are not required
Code:
  sed -ne 's/"//g' -e '2 p' -e '4 p' -e '6 p' paramfilename

about visibility of the file i am not sure what exactly you are trying to say...
you can set permission as 711 i.e. rwx--x--x

Code:
 chmod 711 paramfilename

# 4  
Old 04-16-2012
Sam05121988 - you cannot source a file that only has execute permissions.

One can use sudo to get around this as show below:

create a group secret_params
make your param file owned by root:secret_params with 640 permissions (rw-r----)
In sudoers allow your.script to be run by any user as group secret_params without password:
Code:
ALL  ALL = (:secret_params) NOPASSWD: /usr/local/bin/your.script

At the top of your script: switch to group secret_params if not already in it and then execute script again:
Code:
#!/bin/sh
id=$(id -gn)
if [ $id != "secret_params" ]
then
   exec sudo -g secret_params $0 $@
fi

As far as the format of the params file make it var="value"
Code:
var="testing var"
password="top_secret"

it can be sourced in the script directly eg:
Code:
# read parameter valuse
. /usr/local/lib/myscript.params
echo var=$var

# 5  
Old 04-16-2012
Thanks a lot guys for the prompt response.
I will try the options offered By Chubler and Sam for reading the Parm file.Earlier I have executed the Follwing form of it

Parm.txt had following contents
User="abc"
Pwd="password"
Export $User $Pwd

And script.ksh has
User=$1
Pwd=$2

Then I executed script as

./script.ksh ./Parm.txt

But it didn't work earlier. Will try this new approach. I have changed the permissions on the file already as mentioned here and that's how I manage to keep it protected. I am not sure if sudo is installed on my system. Will try to identify and will work with solutions mentioned. Will be definitely updating here soon with the results. Please let me know if there is a solution otherwise.
Thanks again..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading and copying a selected rows

Dear All, I have a data file input.res like below. (Only six column shown here for example.) Sequence of first column starting from 1 to 148. Input file 1 Q0 9_August_2014_Entertainment2 0 20.14967806339729 BM25b1.0 1 Q0 13_October_2012_Page323 1 20.134224346765738 BM25b1.0 1 Q0... (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

2. Shell Programming and Scripting

Deleting selected lines in a file

Hi Guys , I have two files say a1 and a2 having following contents a1 dag wfd a2 dag wfd chire hcm I want to delete only the lines in a2 which are in a1 and final output of a2 should be a2 chire hcm (6 Replies)
Discussion started by: Pradeep_1990
6 Replies

3. Shell Programming and Scripting

Reading in two lines at once from a text file

Hello everyone, I have thought about this for quite some time and know what I want to do but am having some trouble at it. I have a text file filled with numbers like this, there are more in the file obviously. Each number is separated by a space. 1 3 2 4 5 1 -1 1 0 -1 5The idea is... (7 Replies)
Discussion started by: tastybrownies
7 Replies

4. Shell Programming and Scripting

Selected lines from a file based on another file

Hello, I am using Awk in Ubuntu 12.04 First file: I have a file like this: SNP1 1 198.2 SNP2 1 124.5 SNP3 1 124.4 . . . Second file: I have another file like this: SNP2 SNP5 SNP10 . . . I want to create a third file like my first file but keeping ONLY the SNPs that... (8 Replies)
Discussion started by: Homa
8 Replies

5. Shell Programming and Scripting

Regarding reading lines into a new file

Hi all, I jut use a loop to read lines from the user and redirect it to a file. echo "Enter the line" while read -r LINE do echo $LINE >> FILE if ;then break fi done input app... (1 Reply)
Discussion started by: Ananthdoss
1 Replies

6. Shell Programming and Scripting

Print selected lines from file in order

I need to extract selected lines from a log file, I can use grep to pull one line matching 'x' or matching 'y', how can I run through the log printing both matching lines in order top to bottom. i.e line 1 xyz - not needed line 2 User01 - needed line 3 123 - not needed line 4 Info - needed... (2 Replies)
Discussion started by: rosslm
2 Replies

7. Shell Programming and Scripting

skip lines while reading a file

Hi Experts, I am tryin to read a file and while doing so i need to skip the lines which start with a hash (#) char. I thought of using a goto command but a lot of guys on this site say its not the good way to program. Moreover I am using a ksh shell which deos not support goto command. ... (4 Replies)
Discussion started by: bankimmehta
4 Replies

8. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

9. UNIX for Advanced & Expert Users

Reading a file and writing the file name to a param file.

Hi All, Not sure if this would be in a dummies sectiin or advanced. I'm looking for a script if someone has doen something like this. I have list of files - adc_earnedpoints.20070630.txt adc_earnedpoints.20070707.txt adc_earnedpoints.20070714.txt adc_earnedpoints.20070721.txt... (1 Reply)
Discussion started by: thebeginer
1 Replies

10. UNIX for Dummies Questions & Answers

Script for reading filelist and preparing param file.

Hi All, Not sure if this would be in a dummies sectiin or advanced. I'm looking for a script if someone has doen something like this. I have list of files - adc_earnedpoints.20070630.txt adc_earnedpoints.20070707.txt adc_earnedpoints.20070714.txt adc_earnedpoints.20070721.txt... (2 Replies)
Discussion started by: thebeginer
2 Replies
Login or Register to Ask a Question