How pass the input parameter to a file in the script ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How pass the input parameter to a file in the script ?
# 1  
Old 03-07-2017
How pass the input parameter to a file in the script ?

OS version: RHEL 6.7

myTextFile.txt file is referred within Script1.sh script,

I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ?

Code:
$ cat script1.sh
cat myTextFile.txt

$ cat myTextFile.txt
$1

Requirement1.
When I execute script1 like below, the input parameter should be passed inside myTextFile.txt

Code:
script1.sh <inputParameter>

But, it only prints the following with my current script

Code:
$ ./script1.sh hello
$1

Requiremen2:
I want the value that is passed as the parameter to be enclosed in single quotes in the output. In the above example , the string hello is passed as the parameter and I want the output to be 'hello'

Expected output:
Code:
$ ./script1.sh hello
'hello'

# 2  
Old 03-07-2017
Hi,

As things currently stand, your script is in fact doing precisely what it should. To recap, at the moment your script contains one line of code, namely:

cat myTextFile.txt

So all this script will ever do is display the contents of a file in the current directroy called myTextFile.txt. It does not take any input, accept any parameters, or do anything else. In short, running this script is 100% equivalent to just directly typing cat myTextFile.txt at the shell prompt yourself.

If instead you want a script to return as output whatever single input parameter is passed to it, then you don't need to use a file for this. To get both the requirements you list, something like this would suffice:

Code:
$ cat script.sh 
#!/bin/bash

echo "'$1'"

Here's a sample session of the script being run.

Code:
$ ./script.sh Hello
'Hello'
$ ./script.sh "Hello world"
'Hello world'
$

Hope this helps.
This User Gave Thanks to drysdalk For This Post:
# 3  
Old 03-07-2017
Hello kraljic,

Not sure about your complete requirements, could you please try following and let me know if this helps you.
Code:
cat test.txt
hello

cat script.ksh
awk -vs1="'" '{print s1 $0 s1}' test.txt

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 03-07-2017
Thank You drysdalk.

In my real life scenario, I have program being called inside script1.sh . This program doesn't take any input parameters.
Instead, it reads a config file (config.txt) and executes based on the input parameter. This is why I need to pass the input parameter to config.txt

Code:
$ cat script.sh

<Step1.do something>

<step2.do some more things>

#Step3.
runBridge config.txt

# 5  
Old 03-07-2017
Hi,

Ah, OK - I think I understand. Something like this then, maybe ?

Code:
$ cat config.txt
Hello
$ cat script.sh
#!/bin/bash
file="config.txt"
parameter=`/bin/cat $file`

echo My parameter is "'$parameter'"

$ ./script.sh 
My parameter is 'Hello'
$

There are various ways of doing this kind of thing, but if you always know the file will contain only one single string, and you want to stick to as close to your original script as you can, then this should be sufficient.
This User Gave Thanks to drysdalk For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

2. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (3 Replies)
Discussion started by: Debalina Roy
3 Replies

3. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (2 Replies)
Discussion started by: Debalina Roy
2 Replies

4. Shell Programming and Scripting

How to pass the password as input parameter to scp

Dear all Does anybody know how to pass the password as input parameter to scp or rsync in unix scripts? I have tried echo <password> | scp filename username@<ip address>:/filepath/ . But it does not work. BTW, I dont want to setup ssh trust between servers in this adhoc task. Regards,... (2 Replies)
Discussion started by: eldonlck
2 Replies

5. Shell Programming and Scripting

Pass value from file to parameter

Hi Guys, I have a file in the format Parmater=value. I want to read the value and pass it to corresponding Variable. The Parameter file is as follows Number=23 Text1=mango Text2=yup 'Number' value needs to be read and passed to ID variable. Also, 'Text1' value needs to be passed to... (9 Replies)
Discussion started by: mac4rfree
9 Replies

6. Shell Programming and Scripting

How to pass string as a parameter in a script

Hi friends. i am newbie to shell scripting. I need to create a script where i will be passing 2 parameters to the script and based on that it should work. For ex: start_proc a 2 or start_proc b 2 start_proc a 2 --- this should bring up 2 processes as i define inside the script. start_proc... (2 Replies)
Discussion started by: friscouser
2 Replies

7. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

8. Shell Programming and Scripting

Pass parameter into script

I would like to write a scirpt a.sh that it first checks the first parameter of the input. If it fulfill some condition ,then run an executable program b by using all the parameter. ie. > ./a.sh 10 20 30 40 50 Then a.sh first checks the first parameter, 10, if it mathes the requirement, then... (2 Replies)
Discussion started by: alfredo
2 Replies

9. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies

10. UNIX for Dummies Questions & Answers

Pass Parameter to Another Script

I have a piece of code that I do not want to continuously repeat. I want to call script2 from script1 and pass a parameter. Here is an example: Script1: ....... nohup ./Script2 PARAMETER ....... Script2: if # Checks if any params. then echo "No parameters passed to function." ... (4 Replies)
Discussion started by: rvprod
4 Replies
Login or Register to Ask a Question