![스크립트를 사용한 Linux 자동화 [닫기]](https://linux55.com/image/214375/%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%9C%20Linux%20%EC%9E%90%EB%8F%99%ED%99%94%20%5B%EB%8B%AB%EA%B8%B0%5D.png)
저는 리눅스를 처음 접했습니다. 정기적으로 이 문제를 처리해야 하며 몇 가지 작업을 자동화하는 데 도움이 필요합니다. 자동 스크립트를 작성하고 싶습니다. 스크립트가 실행되면 다음과 같이 실행되어야 합니다.
- 경로에 파일 이름을 입력하겠습니다.
- 해당 경로의 파일을 다른 디렉터리로 복사해야 합니다.
- 이 디렉토리에 복사한 후. tar -xvf filename 명령을 사용하여 압축을 풀어야 합니다.
- ln -s test.tis 테스트에 대한 소프트 링크를 만듭니다.
- 끝.
가능합니까? 모든 예와 자세한 도움이 도움이 될 것입니다.
답변1
스크립트는 확실히 더 잘 작성될 수 있지만, 무엇을 어디서, 무엇으로 추출할지, 어떤 소스에서 어떤 대상으로 연결할지 알지 못하면 더 나은 스크립트를 작성하기가 어렵습니다.
너할 수 없다경로 입력 시 약어(또는 등)를 사용하며 , $HOME
실행 전 ~
필수 입력 사항입니다 .chmod +x NameOfTheScript.sh
먼저 스크립트를 테스트하기 위해 tarball을 만드십시오!테스트 파일과 한 테스트 디렉터리에서 다른 디렉터리로 스크립트를 실행합니다. 예를 들어
# zip a test file
touch deleteme.txt
tar -czvf deleteme.tar.gz deleteme.txt
# create two test-dirs
mkdir -p {testdir1,testdir2}
새 스크립트
최소한 다시 작성하십시오.일부사용자 입력 검증.
#!/bin/bash
printf "================================================================\n
IMPORTANT INFORMATION: \n
- This script will let you extract an archive to a desired \n
location, and link one of the extracted files to a desired \n
location. \n
\n
- Please be aware that entering absolute paths is usually \n
better when linking files, as this protects from path changes \n
in the future. \n
================================================================\n"
read -p "Press enter to continue... "
printf "\n================================================================\n
[INSTRUCTIONS] \n
:: Archives :: \n
- If the file is in the same folder as the script, you may \n
provide only the file name. \n
Ex: myarchive.tar.gz \n
\n
- If the file is in a subdir, you may provide the subdir, and \n
the file name, including its extension. \n
Ex: mydir/myarchive.tar.gz \n
\n
- If the file is in a totally different dir, please provide the \n
full (absolute) path, including file name and extension. \n
Ex: /home/myuser/somedir/somerandomsubdir/myarchive.tar.gz \n
================================================================\n"
read -p "Press enter to continue... "
printf "\n================================================================\n
:: Linking :: \n
ABSOLUTE PATH \n
- Source file - \n
/home/myuser/mydocuments/mysubdir/myfile.txt \n
\n
- Destination - \n
Give link the same name: /home/myuser/mydocuments/mysubdir2 \n
Different name: /home/myuser/mydocuments/mysubdir2/myfile2.txt \n
\n
RELATIVE PATH \n
- Source file - \n
In the same dir: myfile.txt \n
In a subdir: mysubdir/myfile.txt \n
\n
- Destination - \n
In the same dir: myfile.txt \n
In a subdir: mysubdir2 \n
\n================================================================\n"
read -p "Press enter to continue... "
OLDWD=$(pwd)
while true; do
# get user input
read -rp "Path to file (including file name): " PATHVAR
read -rp "Destination: " DESTVAR
PATHPROMPT=$(printf "Do you want to use relative paths or absolute paths?\n(A)bsolute/(R)elative: ")
# get user input
echo
read -rp "$PATHPROMPT" CHOICE
# keeps asking for input until user gets it right
while [ "${CHOICE,,}" != "a" ] && [ "${CHOICE,,}" != "r" ] || [ -z "$CHOICE" ]; do
printf "%s\nBad choice: $CHOICE\n"
read -rp "Please choose 'A' or 'R': " CHOICE
done
# get user input
echo
read -rp "Link source: " LNKSRC
read -rp "Link to destination: " LNKDEST
# echo output to user for verification
printf "%s\nFile to extract: $PATHVAR"
printf "%s\nDestination path: $DESTVAR"
echo
printf "%s\nFile to link: $LNKSRC"
printf "%s\nLink to: $LNKDEST"
echo
# ask user if information entered is correct
read -rp "Is this correct? [(y)es/(N)o/(e)xit] " ANSW
# keeps asking for input until user gets it right
while [ "${ANSW,,}" != "y" ] && [ "${ANSW,,}" != "n" ] && [ "${ANSW,,}" != "e" ] || [ -z "$ANSW" ]; do
printf "%s\nBad choice: $ANSW\n"
read -rp "Please choose 'Y', 'N', 'E': " ANSW
done
case "${ANSW}" in
[Yy] )
# copy file from src to dest
printf "%s\nCopied ""$PATHVAR"" to ""$DESTVAR"
cp -r "$PATHVAR" "$DESTVAR"
# change dir to source dir
cd "$DESTVAR" || return
# extract file
printf "\nExtracted following file(s):"
for path in $PATHVAR; do
tar -xvf "${path##*/}"
done
# change dir to old working dir
cd "$OLDWD" || return
case "${CHOICE}" in
[Aa] )
printf "%s\nLinking ""$LNKSRC"" to ""$LNKDEST"
# create soft link
ln -s "$LNKSRC" "$LNKDEST"
;;
[Rr] )
printf "%s\nLinking ""$LNKSRC"" to ""$LNKDEST"
# create soft link
ln -sr "${DESTVAR}/${LNKSRC}" "$LNKDEST"
printf
;;
esac
# verify that link is correct
printf "\nPlease verify that the link is correct:"
ls --color=auto -l "$LNKSRC" "$LNKDEST"
printf "\nDone."
exit
;;
[Nn] )
printf "\nStarting over...\n"
;;
[Ee] )
printf "\nExiting...\n"
exit
;;
esac
done
오래된 스크립트
#!/bin/bash
# ask user for input
echo "Full path to file (including file name):"
# get user input
read -r PATHVAR
# copy file to destination
echo "Destination:"
# get user input
read -r DESTVAR
# copy file from src to dest
cp -rv "$PATHVAR" "$DESTVAR"
# change dir into dest dir
cd "$DESTVAR" || return
# extract file name
for path in $PATHVAR ; do
tar -xvf "${path##*/}"
done
# ask for softlink name and src
echo "File to link (full path or file name, if file is in current dir):"
# get user input
read -r LNKSRC
# ask for softlink dest
echo "Softlink to dest:"
# get user input
read -r LNKDEST
# create softlink
ln -s "$LNKSRC" "$LNKDEST"
echo "Done."