Passing arguments to a shell script - shell

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

Related

how to supply a file name as an argument to shell script

I want to run a shell script, run.sh
line_no=$(awk '{x++} END {print x}' #file_name#)
echo $line_no
I want to run the script on different files each time. How can supply the file_name as an argument while running this script?
The arguments to a shell script are $0 (the name of the script), $1 (the first argument passed to the script), $2 (the second argument to the script), etc.
So just use $1 in place of your filename!

How to pass shell variables in "echo" command

I have she script as the below content
chr=$0
start=$1
end=$2
echo -e "$chr\t$start\t$end" > covdb_input.bed
How do i pass the chr,Start and end variables in to echo command.. or write same to file "covdb_input.bed" with TAB sep as in echo command.
You're doing everything right, except that you probably initialize your variables with the wrong things.
I'm assuming you get arguments for the script (or shell function), and that you want to use these. Then pick the positional variables from $1 and onwards as $0 will usually contain the name of the current shell script or shell function.
Also, you might find people scoffing about the use of -e with echo (it's a common but non-standard option). Instead of using echo you could use printf like this:
printf "%s\t%s\t%s" "$chr" "$start" "$end" >myfile.bed
Or just
printf "$chr\t$start\t$end" >myfile.bed

bash argval not read

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.

Remove the trailing spaces from command line passed param in bash

I run a command:
script.sh ___bubu__
The content of the script.sh is:
echo $1
When executed I get
___bubu__
How can I remove the trailing spaces from command line passed arguments?
I copy some params from a file and when pasting into command line I get some spaces and I do not want to manually remove the space. I am planning to use $1 as a parameter in a script. For example I want to create a folder with $1
Would something like this work?
..> more strip.sh
#!/bin/bash
arg=$1
arg=${arg// /}
echo "unstripped: --$1--"
echo "stripped: --$arg--"
.03:163> ./strip.sh " test "
unstripped: -- test --
stripped: --test--

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