How to read input from command line? [duplicate] - shell

This question already has answers here:
get the user input in awk
(3 answers)
Closed 4 years ago.
I have to read certain things from the command line to a shell script which calls an awk script. As far as I know, reading into an awk script is not possible, so I would have to read it into the shell script which then would pass the value of the variable to the awk script so that I could work with it. How can it be done?

For example in Bash you use read to read input into a variable and pass the variable to awk with awk's command line parameter:
$ read -p PROMPT var ; awk -v awkvar="$var" 'BEGIN{print "You wrote: " awkvar}'
PROMPTfoo
You wrote: foo

Here you go:-
create a shell script with below information
vi my.sh
Now add below line of script
#!/bin/bash
#add your awk command here with $1
echo $1
while shift
do
#add your awk command here with $i
echo $1
done
Save the file and
chmod 777 my.sh
Now run:-
./my.sh hello world how are you doing
Out put:-
hello
world
how
are
you
doing

Related

Store Result of Bash Command in Shell Variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 months ago.
I am trying to store the result of a bash command within a for loop for use in a command. This is what I currently have:
for filename in /home/WIN/USER/files/*
var=$(basename ${filename%.*}) | awk -F'[_.]' '{print $1}'
do echo var
done
However, I am getting these errors:
./script.sh: line 2: syntax error near unexpected token `var=$(basename ${filename%.*})'
./script.sh: line 2: `var=$(basename ${filename%.*}) | awk -F'[_.]' '{print $1}''
Does anyone know how to fix this or how to do what I am trying to do?
Thanks.
Your for statement is wrong, and your variable assignment statement is also wrong. You shall write something like this:
for filename in /home/WIN/USER/files/*; do
var=$( your shell code goes here ) # you assign the output of the shell code here
echo "$var" # you echo the results here
done

How do I pipe into printf? [duplicate]

This question already has answers here:
Piping not working with echo command [duplicate]
(4 answers)
How to pass command output as multiple arguments to another command
(5 answers)
Closed 1 year ago.
I'm going nuts trying to understand what is the problem with this simple example (zsh or bash):
echo -n "6842" | printf "%'d"
The output is 0... why though? I'd like the output to be 6,842
Thanks in advance, I've had no luck for an hour now using google trying to figure this out...!
printf doesn't read arguments to format from standard input, but from the command line directly. For example, this works:
$ printf "%'d" 6842
6,842
You can convert output of a command to command-line arguments using command substitution:
$ printf "%'d" $(echo -n 6842)
6,842
If you want to invoke printf inside a pipeline, you can use xargs to read input and execute printf with the appropriate arguments:
echo -n "6842" | xargs printf "%'d"
printf does not format data passed to it on standard input; it takes a set of arguments, the first of which is the format, and the remainder are the values to display.
Luckily, this is exactly what xargs is for; to quote the manual:
xargs - build and execute command lines from standard input
So instead of piping to printf directly, you can pipe to xargs, and tell it to run printf for you with the given arguments. In short:
echo -n "6842" | xargs printf "%'d"

awk and bash script? [duplicate]

This question already has answers here:
Syntax error in shell script with process substitution
(4 answers)
Closed 3 years ago.
I wonder why it doesn't work.
Please advise me.
1. working
$ nu=`awk '/^Mem/ {printf($2*0.7);}' <(free -m)`
$ echo $nu
1291.5
2. not working
$ cat test.sh
#!/bin/bash
nu=`awk '/^Mem/ {printf($2*0.7);}' <(free -m)`
echo $nu
$ sh test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `awk '/^Mem/ {printf($2*0.7);}' <(free -m)'
Could you please try following.
nu=$(free -m | awk '/^Mem/ {print $2*0.7}')
echo "$nu"
Things taken care are:
Use of backtick is depreciated so use $ to store variable's value.
Also first run free command pass its standard output as standard input to awk command by using |(which should be ideal way of sending output of a command to awk in this scenario specially) and save its output to a variable named nu.
Now finally print variable nu by echo.
Since <(...) process substitution is supported by bash not by sh so I am trying to give a solution where it could support without process substitution (which I mentioned a bit earlier too).
The <( ) construct ("process substitution") is not available in all shells, or even in bash when it's invoked with the name "sh". When you run the script with sh test.sh, that overrides the shebang (which specifies bash), so that feature is not available. You need to either run the script explicitly with bash, or (better) just run it as ./test.sh and let the shebang line do its job.
The reason to add a shebang in a script is to define an interpreter directive if the file has execution permission.
Then, you should invoke it by, for example
$ ./test.sh
once you have set the permission
$ chmod +x test.sh

How to store output of a awk script command into a variable? [duplicate]

This question already has answers here:
Shell script, saving the command value to a variable
(1 answer)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I have an awk script, testscript.script, with command:
$5 > 0 {print $1}
which outputs
Lebron
Kobe
James
Tony
How can I store this command into a variable var such that when I say print var at any point in the code, the above output will print?
You need command substitution, $():
var=$(awk ...)
The STDOUT of the awk command will be saved in variable var.
Now, you can do:
echo "$var"
to get the output.

How to find out name of script called ("sourced") by another script in bash? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
In the bash script how do I know the script file name?
How can you access the base filename of a file you are sourcing in Bash
When using source to call a bash script from another, I'm unable to find out from within that script what the name of the called script is.
file1.sh
#!/bin/bash
echo "from file1: $0"
source file2.sh
file2.sh
#!/bin/bash
echo "from file2: $0"
Running file1.sh
$ ./file1.sh
from file1: ./file1.sh # expected
from file2: ./file1.sh # was expecting ./file2.sh
Q: How can I retrieve file2.sh from file2.sh?
Change file2.sh to:
#!/bin/bash
echo "from file2: ${BASH_SOURCE[0]}"
Note that BASH_SOURCE is an array variable. See the Bash man pages for more information.
if you source a script then you are forcing the script to run in the current process/shell. This means variables in the script from file1.sh you ran are not lost. Since $0 was set to file1.sh it remains as is.
If you want to get the name of file2 then you can do something like this -
file1.sh
#!/bin/bash
echo "from file1: $0"
./file2.sh
file2.sh
#!/bin/bash
echo "from file2: $0"

Resources