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 ?

$ 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

script1.sh <inputParameter>

But, it only prints the following with my current script

$ ./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:

$ ./script1.sh hello
'hello'

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:

$ cat script.sh 
#!/bin/bash

echo "'$1'"

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

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

Hope this helps.

1 Like

Hello kraljic,

Not sure about your complete requirements, could you please try following and let me know if this helps you.

cat test.txt
hello

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

Thanks,
R. Singh

1 Like

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

$ cat script.sh

<Step1.do something>

<step2.do some more things>

#Step3.
runBridge config.txt

Hi,

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

$ 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.

1 Like