LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-19-2015, 08:12 AM   #1
prasad1990
LQ Newbie
 
Registered: Feb 2015
Posts: 11

Rep: Reputation: Disabled
How to pass string variable as command parameter in shell script?


Hello Friends,

I want to pass string variable with its value taken by user as command parameter in shell script, example below:-

echo "Enter New Password:="
read password


perl -pi -e
's/PASSWORD=.*?,/$password,/' abc.txt

Please let me know...
Thanks In advanced.
 
Old 03-19-2015, 09:13 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
How about:
Code:
CMD="perl -pi -e 's/PASSWORD=.*?,/"$password",/' abc.txt"
eval "$CMD"
 
Old 03-19-2015, 05:04 PM   #3
drdvp
LQ Newbie
 
Registered: Mar 2014
Posts: 1

Rep: Reputation: Disabled
It should also work if you replace the single quotes with double quotes:
perl -pi -e "s/PASSWORD=,/$password," abc.txt
 
Old 03-20-2015, 12:06 AM   #4
prasad1990
LQ Newbie
 
Registered: Feb 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks to both of you.
There are multiple password field in file, and i want replace only one field out of multiple field. I used your solution but it replace all field.

What should i do?

Last edited by prasad1990; 03-20-2015 at 12:07 AM.
 
Old 03-20-2015, 09:05 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
What is your program?

Parameters are stored in the argv array - and it is trivial to add something like "password=value" on the command line.
 
Old 03-20-2015, 10:25 AM   #6
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
What identifies the place in the file that you want to change?
 
Old 03-23-2015, 12:48 AM   #7
prasad1990
LQ Newbie
 
Registered: Feb 2015
Posts: 11

Original Poster
Rep: Reputation: Disabled
Actually, I have one file which contain connector with parameter,and want to replace only activate connector PASSWORD with this format NEW_PASSWORD=$password, password value accept by the user. for example below:


echo "Enter New Password:="
read password


perl -pi -e
's/PASSWORD=.*?,/$password,/' abc.txt


This is may abc.txt:-
# Activate connector
3[USER-ID=abacsd,PASSWORD=1234568,member-id=psudfsfd, etc..]

# DeActivate connector
;8[USER-ID=fdsdfgs,PASSWORD=45646224,member-id=psudfsfd, etc..]


Updated abc.txt
# Activate connector
3[USER-ID=abacsd,NEW_PASSWORD=1545644,member-id=psudfsfd, etc..]

# DeActivate connector
;8[USER-ID=fdsdfgs,PASSWORD=45646245,member-id=psudfsfd, etc..]

What should i do?

Last edited by prasad1990; 03-23-2015 at 12:52 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
how to pass command-line parameter to shell script? Kropotkin Linux - Newbie 12 07-25-2011 09:24 AM
Pass a shell variable to an AWK command chogall Programming 1 12-23-2010 10:12 AM
[SOLVED] Pass space (or string escaped) as argument to shell script. scmbg Programming 1 10-07-2009 10:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration