私はLinuxに初めて触れました。定期的にこの問題を処理する必要があり、いくつかのタスクを自動化するのに役立ちます。自動スクリプトを作成したいです。スクリプトが実行されると、次のように実行する必要があります。
- パスにファイル名を入力します。
- そのパスのファイルを別のディレクトリにコピーする必要があります。
- このディレクトリにコピーした後。 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."