Programing

출력을 위해 줄 번호를 추가하고 줄을 묻고 입력에 따라 행동하는 방법은 무엇입니까?

lottogame 2020. 11. 29. 09:31
반응형

출력을 위해 줄 번호를 추가하고 줄을 묻고 입력에 따라 행동하는 방법은 무엇입니까?


다음과 같은 쉘 스크립트를 작성했습니다.

#! /bin/sh
...
ls | grep "android"
...

출력은 다음과 같습니다.

android1 
android2
xx_android
...

다음과 같이 각 파일에 번호를 추가하고 싶습니다.

    1 android1 
    2 android2
    3 XX_android
    ...
    please choose your dir number:

그런 다음 사용자 입력 줄 번호 x를 기다립니다. 스크립트는 줄 번호를 다시 읽은 다음 해당 디렉터리를 처리합니다. 쉘에서 어떻게 할 수 있습니까? 감사 !


상호 작용을 구현하는 대신 내장 명령을 사용할 수 있습니다 select.

select d in $(find . -type d -name '*android*'); do
    if [ -n "$d" ]; then
        # put your command here
        echo "$d selected"
    fi
done

nl은 줄 번호를 인쇄합니다.

ls | grep android | nl

결과를으로 파이프하면 옵션을 cat사용하여 -n다음과 같이 각 줄에 번호를 지정할 수 있습니다 .

ls | grep "android" | cat -n

패스 -ngrep다음과 같이 :

ls | grep -n "android"

로부터 grep인간 페이지 :

 -n, --line-number
        Prefix each line of output with the line number within its input file.

이것은 나를 위해 작동합니다.

line-number=$(ls | grep -n "android" | cut -d: -f 1)

Googlebot이 크롤링하지 않도록하는 내 sitemap.xml의 섹션을 제거하기 위해 스크립트에서 이것을 사용합니다. 고유 한 URL을 검색 한 다음 위를 사용하여 줄 번호를 찾습니다. 간단한 수학을 사용하여 스크립트는 XML 파일의 전체 항목을 삭제하는 데 필요한 숫자 범위를 계산합니다.

더 나은 답변을 얻기 위해 귀하의 질문을 업데이트하는 것에 대해 jweyrich와 동의합니다.


이 페이지의 다른 답변은 실제로 질문에 100 % 답변하지 않습니다. 사용자가 다른 스크립트에서 파일을 대화식으로 선택하도록하는 방법은 표시하지 않습니다.

다음 접근 방식을 사용하면 예제에서 볼 수 있듯이이를 수행 할 수 있습니다. 점을 유의 select_from_list스크립트가 당겨졌다 이 유래 게시물에서

$ ls 
android1        android4        android7        mac2            mac5
android2        android5        demo.sh         mac3            mac6
android3        android6        mac1            mac4            mac7

$ ./demo.sh
1) android1  3) android3  5) android5  7) android7
2) android2  4) android4  6) android6  8) Quit
Please select an item: 3
Contents of file selected by user: 2.3 Android 1.5 Cupcake (API 3)

다음 demo.sh은 목록에서 항목을 선택하는 데 사용하는 스크립트입니다.select_from_list.sh

demo.sh

#!/usr/bin/env bash

# Ask the user to pick a file, and 
# cat the file contents if they select a file.
OUTPUT=$(\ls | grep android | select_from_list.sh | xargs cat)
STATUS=$? 

# Check if user selected something
if [ $STATUS == 0 ]
then
  echo "Contents of file selected by user: $OUTPUT"
else
  echo "Cancelled!"
fi

select_from_list.sh

#!/usr/bin/env bash

prompt="Please select an item:"

options=()

if [ -z "$1" ]
then
  # Get options from PIPE
  input=$(cat /dev/stdin)
  while read -r line; do
    options+=("$line")
  done <<< "$input"
else
  # Get options from command line
  for var in "$@" 
  do
    options+=("$var") 
  done
fi

# Close stdin
0<&-
# open /dev/tty as stdin
exec 0</dev/tty

PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do 
    if (( REPLY == 1 + ${#options[@]} )) ; then
        exit 1

    elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
        break

    else
        echo "Invalid option. Try another one."
    fi
done    
echo $opt

참고URL : https://stackoverflow.com/questions/9443694/how-to-add-line-number-for-output-prompt-for-line-then-act-based-on-input

반응형