This question already has answers here:
How can I loop over the output of a shell command?
(4 answers)
Closed 2 months ago.
i am building a bash script that is supposed to put each line of the output of one command to an variable and then run some commands on that, i am doing it like
for i in `cmd`
do
echo i=$i
lang=$(echo $i | cut -d '"' -f 6)
echo lang=$lang
#some stuff
done
my problem is that for is using space and newlines for separation to different $i's and i want it to do create only new $i's with newline delimiters cause every line may have a cupple of spaces and i want them no matter that handled as it own...
google but found nothing really helping me, only suggestions to use xargs which dosnt help me cause i need to use not one command but a cupple after creating some variables and running some if statements that desiside which command is to run if any...
If you want to read cmd's output line by line you can do it using
while loop and bash's internal read command
cmd | while IFS= read -r i
do
echo "i=${i}"
lang="$(echo "${i}" | cut -d '"' -f 6)"
echo "lang=${lang}"
#some stuff
done
Use " around a variable's de-reference to avoid problems with spaces inside it's value.
Related
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"
This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm new to bash script, it is interesting, but somehow I'm struggling with everything.
I have a file, separated by tab "\t" with 2 infos : a string and a number.
I'd like to use both info on each line into a bash script in order to look into a file for those infos.
I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.
#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}
and my file is :
ABCD\t1120
ABC\t1121
I'm launching my command with
./script.sh file_of_data.tsv
What I finally get is :
script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found
I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(
Hope you can help me :)
Best,
The quotes cause the shell to look for a command whose name is the string between the quotes.
Apparently you are looking for
while IFS=$'\t' read -r id hotspot; do
echo "read id is: $id with position $hotspot"
done <"$1"
You generally want to avoid capturing things into variables you only use once, but the syntax for that is
id=$(echo "$line" | cut -f1)
See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).
You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.
This question already has answers here:
Preserving leading white space while reading>>writing a file line by line in bash
(5 answers)
Closed 6 years ago.
I need to create a file by modifying some lines of a source one.
I developed a loop 'while read line; do'. Inside it, the lines I read and don't modify go just:
echo -e "$line" >> "xxxx.c"
My issue is that some of that lines start with '\t', and they won't print the output file.
Example:
while read line;
do
if echo "$line" | grep -q 'timeval TIMEOUT = {25,0};'
then
echo "$line"
fi
Any help? I've tried with the printf command also but without success.
In that case you could just remove "-e" argument from the echo command.
From echo man page:
-e enable interpretation of backslash escapes
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 7 years ago.
I am doing basic programs with Linux shell scripting(bash)
I want to read the first line of a file and store it in a variable .
My input file :
100|Surender|CHN
101|Raja|BNG
102|Kumar|CHN
My shell script is below
first_line=cat /home/user/inputfiles/records.txt | head -1
echo $first_line
I am executing the shell script with bash records.sh
It throws me error as
/home/user/inputfiles/records.txt line 1: command not found
Could some one help me on this
The line
first_line=cat /home/user/inputfiles/records.txt | head -1
sets variable first_line to cat and then tries to execute the rest as a command resulting in an error.
You should use command substitution to execute cat .../records.txt | head -1 as a command:
first_line=`cat /home/user/inputfiles/records.txt | head -1`
echo $first_line
The other answer addresses the obvious mistake you made. Though, you're not using the idiomatic way of reading the first line of a file. Please consider this instead (more efficient, avoiding a subshell, a pipe, two external processes among which a useless use of cat):
IFS= read -r first_line < /home/user/inputfiles/records.txt
This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
Closed 7 years ago.
I want the program can remove punctuation which read from the standard input
My code is:
echo $* | tr -d '[:punct:]'
It can handle some simple situations but when I type input in terminal (like: whatever ad!":)
when the sentence within continuing several punctuation, the terminal will reflect the result: -bash: !": event not found
Can anyone give help?
single quotes should be used to avoid expansion, e.g.
echo 'whatever ad!' | tr -d '[:punct:]'
under a bash shell it prints out
whatever ad
and if you want to use a variable
BUFF='whatever ?_-!!!!ad!'; echo "$BUFF" | tr -d '[:punct:]'
EDIT 1
this is a complete script following your request
#!/bin/sh
functionStripAndPrint()
{
echo "$#" | tr -d '[:punct:]'
}
functionStripAndPrint "$#"
assuming that this script is stored in the stripchars.sh file, you can invoke it like so
./stripchars.sh 'das !adsa _sda ssad-'
and it will print
das adsa sda ssad
EDIT 2
you can work around the interpretation of some of the special characters with set, for example
set +H
deactivates the H option which is linked to the ! symbol, so now ! is just an exclamation mark with no special meaning. You can then simplify your invocation a little bit
./stripchars.sh sdfsa!fdsaf?\'
as you can see the only problem at this point is the ' that still needs to be escaped.
If you want to re-enable the H you do
set -H
set is handy to modify the behaviour of your shell, I don't know if it's worth in your case, the shell is good and handy for some basic stuff, but I don't know if this will fit your needs, you know better, take a look at set and see if it's enough.
As you probably know, bash uses ! to get commands from the history of commands. When you type
echo Whatever ad!":
it tries to retrieve the command from its command history by using !":. Since it does not find any command using that, it prints the message
bash: !": event not found
You can pass those special characters to bash by (1) using single quote to let special characters be treated like normal characters, or (2) escaping the special characters.
echo 'Whatever ad!":' | tr -d '[:punct:]'
echo Whatever ad\!\": | tr -d '[:punct:]'