Using Linux How to Pass the Contents of a File as a Parameter to an Executable

using linux how can I pass the contents of a file as a parameter to an executable?

This should do the trick:

./program `cat file`

If you want the entire file content in a single argument, add double quotation (tested in bash. I think It may vary shell to shell) :

./program "`cat file`" 

How do I use the lines of a file as arguments of a command?

If your shell is bash (amongst others), a shortcut for $(cat afile) is $(< afile), so you'd write:

mycommand "$(< file.txt)"

Documented in the bash man page in the 'Command Substitution' section.

Alterately, have your command read from stdin, so: mycommand < file.txt

Pass content from .txt file as argument for bash script?

$(command) returns the output of the command. If you do $(cat some_file) it will return the text of the file. You can use it to give the content of a file as an argument doing:

cmd1 $(cat args_file)

So when you use echo $(cat file.txt), you get the same output as if you were using cat file.txt because cat sends the content of the file to echo which displays it to the standard output.

$n means argument n passed to the script ($0 being the name of the script). Here you simply have to provide one argument, the name of the file. So $2, $3 and $4 will not contain anything.

So, from the file you can only get a string with the names with $names=$(cat $1). In order to get each field separately, you can use cut:

lname=$(cut -d \  -f 1 $1)
fname=$(cut -d \ -f 2 $1)
mname=$(cut -d \ -f 3 $1)
group=$(cut -d \ -f 4 $1)

NOTES:

The symbol for comments in shell is # NOT //.

head displays the first lines of a file, head -c the first bytes. It does not cut the file.

How to pass command line parameters from a file

With most shells, you can insert the contents of a file into a command line with $(<filename):

./myprogram $(<arguments.dat)

If your shell doesn't support that, then one of the older ways will work:

./myprogram $(cat arguments.dat)
./myprogram `cat arguments.dat` # need this one with csh/tcsh

(You do know the difference between command line arguments and file input, right? Why would you expect to pipe command line arguments into a program?)

How to pass parameters to a .run file in linux

It depends on the file (and there is no reason that every *.run installer behave similarly). Try Talend-Installer-20150508_1414-V5.6.2-linux64-installer.run --help or perhaps Talend-Installer-20150508_1414-V5.6.2-linux64-installer.run -h and read its documentation... sometimes there is an option to accept the license. You might also consider using yes(1) in a pipe:

yes | yourfile.run

But be cautious. What if yourfile.run asked politely:

can I remove every file in /home/ ? [yN]

(Of course, as for any script or executable, you'll need to enable executability and reading with chmod u+rx and either change your PATH or use ./yourfile.run or its absolute or relative file path, etc...)

You might also try to use strings(1) on that executable, to perhaps guess (thru some string messages inside), what is possible.

Argument passing is done thru execve(2) and your shell is in charge of globbing -before doing execve- so there is nothing specific about running *.run files.

I strongly suggest to take a few days to learn more about Linux. Perhaps read first Advanced Linux Programming & Advanced Bash Scripting Guide (and of course, documentation of Chef and of the Talend product you are installing); if you experiment sysadmin things without understanding, you might mess your system to the point of losing data and having to reinstall everything. Both echo(1) & strace(1) might also be useful.

how to run the program and pass arguments to the program using bash file on linux

In linux

save this in a .sh file

/path/to/executable arg1 arg2

Then run

./file.sh 

with proper permission

How to pass the contents of a file as a parameter on the command line?

Assuming fizz contains a single line with no whitespace, the one-liner

for /F %i in (fizz) do @foo %i

will do what you want. If the argument you're putting into fizz may contain whitespace, use

 for /F "delims=" %i in (fizz) do @foo "%i"

to pass the argument in one piece. Double the % characters if putting this into a batch file.

using a Bash variable in place of a file as input for an executable

You can use the following construct:

<(command)

So, to have bash create a FIFO with the command as the output for you, instead of your attempted -i ["${inputData}"], you would do:

-i <(echo "$inputData")

Therefore, here is your final total command:

executable -v -i <(echo "$inputData") -o outputFile.eps

How to pass integers values to a C program executable file through shell script?

< and > do redirection from and to files, not from strings.

| creates a pipeline between processes, and echo outputs to standard output.

You want the pattern

echo 1 2 3 | program


Related Topics



Leave a reply



Submit