bash argval not read - bash

Here is the content of my bash script:
#!/bin/bash
set INPFILE=$argv[1]
echo "Your input file is "
echo $INPFILE
and I execute it as:
$ ./script.sh inputfile.in
For some reason, bash doesn't accept the argval[1]. I do not receive an error either.
BTW, the output is:
Your input file is

Change set INPFILE=$argv[1] to:
INPFILE="$1"
$1, $2, $3 .... etc are the positional parameters passed to the script/function.
See the manual for more info.

Related

How to replace bash variable with command line arguments

My bash script bash.sh only contains one line
echo "${abc:=123}"
I learned that := is used to assign default values. So when I run bash.sh abc=abc, I expect the output to be abc.
However, the output is still 123.
Why is that? Am I call the script in the wrong way? Thanks.
Bash positional arguments are set to $1, $2, etc. Change your script to:
abc=$1
echo "${abc:=123}"
this will make it so if the variable abc is unset the default value is echoed but if another value is passed on the command line abc will be set to that value.
You are passing a parameter and expecting to see it in an environment variable.
If you want to set an environment variable, you can do that before the script name:
$ cat foo
#!/bin/bash
echo "${abc:=123}"
$ ./foo
123
$ abc=hello ./foo
hello

Script to print pass arguments to script

I am using simple script to print the arguments. But not able to do so.
i am using cat command to add content to a file.
[root#cen06gst ~]# cat<<EOF>pass.sh
echo " you have passed me" $#
> EOF
But when i am seeing the file content again using cat , this is showing
[root#cen06gst ~]# cat pass.sh
echo " you have passed me"
Cat only is a command line tool to concatenate and print to the screen, it doesn't modify the file. Read
man cat
If you want to run your script, run
./pass.sh argument
It is also good practice to start your script with a shebang:
#!/bin/bash
Without it the system doesn't know what language to use to process the script.

Passing arguments to a shell script

I want to execute a shell script using usage() in the script.
I have to give three parameters in the usage block like below:
./script_name server_name path flag
How do I write the code block for this?
Finally, I need to read all the three parameters in a line to execute a jar file.
once you pass parameters you can access them with $1, $2 ... $n and $# to get everything at once.
./script_name server_name path flag
In the script code
echo $1, $2, $3
echo "$#" #will print the same thing

How to send positional parameters to output

How do I send $1,$2 etc to output in a script. I want to have a script that takes 3 arguments, a sedsrc file, a awksrc file, and a datainput file. I want the sed call to send the output to the awk. I need the awk call to take that input. Then I need the new output to be redirected into a new output file.
Ive tried
$1>temp.txt
$2>temp2.txt
You use the echo command:
echo "$1" >temp.txt
echo "$2" >temp2.txt
If you want the contents of the files named in the positional parameters, use cat:
cat "$1" >temp.txt
cat "$2" >temp2.txt

how to pass file as an argument to the script file

I have a shell script written in bash and this script should take file as an argument,can any one tell me how to write script for this any ideas on this are apprecited
Thanks,
You can access the command line arguments passed to your script using positional parameters.
Also to check if the right number of arguments have been passed to the script, you can make use of the variable $# which holds the number of arguments passed.
if [ $# -eq 1 ]; then
# exactly 1 argument was passed..use it..its available in $1
echo "Argument $1"
else
# either 0 or >1 arguments were passed...error out.
echo "Incorrect number of arguments passed"
exit 1
fi
Sample run:
$ bash a.sh
Incorrect number of arguments passed
$ bash a.sh foo
Argument foo
$ bash a.sh foo bar
Incorrect number of arguments passed
$
If you need to operate on the file, you can take the name of the file as an argument and just use the file with the specified name.
If you just need to read the contents of the file, you can use redirection to have the script read the contents of the file on standard in. You can do this using ./script < inputfile

Resources