"Command Not Found" Piping a Variable to Cut When Output Stored in a Variable

Command not found piping a variable to cut when output stored in a variable

You need to pass an input string to the shell command using a pipeline in which case cut or any standard shell commands, reads from stdin and acts on it. Some of the ways you can do this are use a pipe-line

dir2=$(echo "$MY_DIR" | cut -d'/' -f-4)

(or) use a here-string which is a shell built-in instead of launching a external shell process

dir2=$(cut -d'/' -f-4 <<< "$MY_DIR")

How to bring the output of a cut-command into a variable?

When you assign to EP, the first section, $fixedFilePath leaves nothing on stdout for the pipe to take. What it does is executes the contents of that variable. You need to echo.

echo $fixedFilePath | rev | cut -d '.' -f 1 | rev

Now to capture that output you need to execute it as part of the assignment. There are various ways to go about this but what I found worked in your case was surrounding it in backticks:

EP=`echo $fixedFilePath | rev | cut -d '.' -f 1 | rev`

bash script use cut command at variable and store result at another variable

The awk solution is what I would use, but if you want to understand your problems with bash, here is a revised version of your script.

#!/bin/bash -vx

##config file with ip addresses like 10.10.10.1:80
file=config.txt

while read line ; do
##this line is not correct, should strip :port and store to ip var
ip=$( echo "$line" |cut -d\: -f1 )
ping $ip
done < ${file}

You could write your top line as

for line in $(cat $file) ; do ...

(but not recommended).

You needed command substitution $( ... ) to get the value assigned to $ip

reading lines from a file is usually considered more efficient with the while read line ... done < ${file} pattern.

I hope this helps.

Getting a piped curl command output to a variable in bash

You need to remove the -o option that writes the output to a file, in your case /dev/null. Here is your updated code that should work.

$ response=$(curl -k -x $i:1234 -U $u:$p -w $outputformat -s $site | cut -d ' ' -f 2)

Another way to check is to remove pipe and send your STDERR to /dev/null. This will show you what is getting passed to your cut command.

You should see something if all is happy.

$ curl -k -x $i:1234 -U $u:$p  -s $site 2>/dev/null

If you don't get any output, then remove the STDERR redirect and echo the return code.

$ curl -k -x $i:1234 -U $u:$p  -s $site
$ echo $?
0

If the exit code is anything other then 0 you have an error. You can find the error codes in the man page or this site has pretty good write up.

No to get the response code.

$ curl  -w "HTTP Response: %{http_code} - URL: %{redirect_url}\n"  -o /dev/null  -s http://yahoo.com/
HTTP Response: 301 - URL: https://yahoo.com/
$ curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/
301
$ response=$(curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/)
$ echo $response
301
$ curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/ | cut -d' ' -f3
301
$ response=$( curl -w "%{http_code}\n" -o /dev/null -s http://yahoo.com/ | cut -d' ' -f3)
$ echo $response
301

how do I store the output of a cut -c command into a variable in shell script

Try

my_var="$( echo  $filename | cut -c 15-20 )"

Demo:

$filename=Incoming_file_180420053826.csv 
$my_var="$( echo $filename | cut -c 15-20 )"
$echo $my_var
180420
$

Piping output from cut

Hi your first option did not work as you have used cut before while loop because cut will filtered one line at a time.

tail -f /var/log/newusers | egrep --line-buffered "ssh-rsa" | cut -d' ' -f7,8 | while read line ; do echo $line ; done

In your second option you have output all the filtered lines to while loop

tail -f /var/log/newusers | egrep --line-buffered "ssh-rsa" | while read line ; do

Can't output Awk to variable, says permission is denied

You should use echo for variable printing like as follows:

a="one two three four five six"
N=2
b=$(echo "$a" | awk -v N=$N '{print $N}')
echo $b

OR use like <<<"$var" for sending input to awk command:

a="one two three four five six"
N=2
b=$(awk -v N=$N '{print $N}' <<<"$a")
echo $b

When we are running command without echo it is considered as a command which is NOT so we get error like line 3: one: command not found so to send Input to awk command use anyone of the above mentioned commands.

How can pass the value of a variable to the standard input of a command?

Simple, but error-prone: using echo

Something as simple as this will do the trick:

echo "$blah" | my_cmd

Do note that this may not work correctly if $blah contains -n, -e, -E etc; or if it contains backslashes (bash's copy of echo preserves literal backslashes in absence of -e by default, but will treat them as escape sequences and replace them with corresponding characters even without -e if optional XSI extensions are enabled).

More sophisticated approach: using printf

printf '%s\n' "$blah" | my_cmd

This does not have the disadvantages listed above: all possible C strings (strings not containing NULs) are printed unchanged.



Related Topics



Leave a reply



Submit