Programing

쉘에서 변수로 출력을 어떻게 리디렉션합니까?

crosscheck 2020. 5. 17. 19:37
반응형

쉘에서 변수로 출력을 어떻게 리디렉션합니까? [복제]


이 질문에는 이미 답변이 있습니다.

나는 그런 스크립트를 가지고있다

genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5

변수에서 genhash에 의해 생성 된 스트림을 얻고 싶습니다. $hash조건부 내부에서 비교하기 위해 변수로 리디렉션하는 방법은 무엇입니까?

if [ $hash -ne 0 ]
  then echo KO
  exit 0
else echo -n OK
  exit 0
fi

$( ... )구문을 사용하십시오 .

hash=$(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5)

TL; DR

가게에 "abc"$foo:

echo "abc" | read foo

그러나 파이프는 포크를 생성하므로 파이프가 $foo끝나기 전에 사용해야 합니다.

echo "abc" | ( read foo; date +"I received $foo on %D"; )

물론, 다른 모든 답변은 OP가 요청한 것을 수행하지 않는 방법을 보여 주지만 OP의 질문을 검색 한 나머지 우리를 실제로 망칠 수 있습니다.

질문에 대한 대답은 read명령 을 사용하는 것 입니다.

방법은 다음과 같습니다.

# I would usually do this on one line, but for readability...
series | of | commands \
| \
(
  read string;
  mystic_command --opt "$string" /path/to/file
) \
| \
handle_mystified_file

수행중인 작업과 중요한 이유는 다음과 같습니다.

  1. series | of | commands이 명령은 매우 복잡한 일련의 파이프 명령 이라고 가정합시다 .

  2. mystic_command파일 경로 대신에 표준 입력 (stdin)으로 파일의 내용을 받아 들일 수 있지만, 없는 --opt인수는 따라서 변수로 와야합니다. 이 명령은 수정 된 내용을 출력하며 일반적으로 파일로 리디렉션되거나 다른 명령으로 파이프됩니다. (예를 들어 sed, awk, perl, 등)

  3. read stdin을 가져 와서 변수에 넣습니다. $string

  4. 퍼팅 read과를 mystic_command괄호를 통해 "서브 셀"로하는 것은 불필요하지만 2 명령 것처럼 연속 파이프와 같은 유동하게 여기서 개별 스크립트 파일이다.

항상 대안이 있으며,이 경우 대안은 위의 예와 비교할 때 추악하고 읽을 수 없습니다.

# my example above as a oneliner
series | of | commands | (read string; mystic_command --opt "$string" /path/to/file) | handle_mystified_file

# ugly and unreadable alternative
mystic_command --opt "$(series | of | commands)" /path/to/file | handle_mystified_file

내 방식은 전적으로 연대순 이고 논리적입니다. 대안은 4 번째 명령으로 시작하여 명령 1, 2 및 3을 명령 대체에 적용합니다.

이 스크립트 에는 실제 예제가 있지만 위의 예제로는 사용하지 않았습니다. 왜냐하면 다른 미친 / 혼란 / 혼란스러운 bash 마술도 있기 때문입니다.


read hash < <(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5)

이 기술은 Bash의 " 프로세스 대체 "를 사용 하여 " 명령 대체 " 와 혼동하지 않습니다 .

다음은 좋은 참고 자료입니다.


호환되는 방식으로 생각합니다.

hash=`genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5`

하지만 저는 선호합니다

hash="$(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5)"

If a pipeline is too complicated to wrap in $(...), consider writing a function. Any local variables available at the time of definition will be accessible.

function getHash {
  genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5
}
hash=$(getHash)

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Functions


You can do:

hash=$(genhash --use-ssl -s $IP -p 443 --url $URL)

or

hash=`genhash --use-ssl -s $IP -p 443 --url $URL`

If you want to result of the entire pipe to be assigned to the variable, you can use the entire pipeline in the above assignments.


I got error sometimes when using $(`code`) constructor.

Finally i got some approach to that here: https://stackoverflow.com/a/7902174/2480481

Basically, using Tee to read again the ouput and putting it into a variable. Theres how you see the normal output then read it from the ouput.

is not? I guess your current task genhash will output just that, a single string hash so might work for you.

Im so neewbie and still looking for full output & save into 1 command. Regards.


Create a function calling it as the command you want to invoke. In this case, I need to use the ruok command.

Then, call the function and assign its result into a variable. In this case, I am assigning the result to the variable health.

function ruok {
  echo ruok | nc *ip* 2181
}

health=echo ruok *ip*

참고URL : https://stackoverflow.com/questions/2559076/how-do-i-redirect-output-to-a-variable-in-shell

반응형