![Bash 스크립트에서 주어진 값의 매개변수 위치 가져오기](https://linux55.com/image/205297/Bash%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%EC%A3%BC%EC%96%B4%EC%A7%84%20%EA%B0%92%EC%9D%98%20%EB%A7%A4%EA%B0%9C%EB%B3%80%EC%88%98%20%EC%9C%84%EC%B9%98%20%EA%B0%80%EC%A0%B8%EC%98%A4%EA%B8%B0.png)
해당 값을 사용하여 매개변수의 위치를 얻는 방법은 무엇입니까?
예를 들어:
myScript.sh hello world
echo "$1"
hello
이 경우 1인 'hello' 위치를 어떻게 얻을 수 있나요?
답변1
더 빠르고 쉬운 방법이 있다고 해도 놀라지는 않겠지만 다음과 같이 작동할 것입니다.
#!/bin/bash
pos=0
found=no
for arg ; do
let pos++
if [[ "$arg" == "hello" ]] ; then
found=yes
break
fi
done
if [[ "$found" == "yes" ]] ; then
echo "Found hello at position $pos"
else
echo "hello not found"
fi