Linux 使用脚本实现自动化

Linux 使用脚本实现自动化

我是 Linux 新手。我必须定期处理它,并且需要一些帮助来自动化一些事情。我想写一个自动脚本。执行该脚本时,它应该按以下方式执行:

  1. 我将输入某个路径中的文件名。
  2. 它应该将该文件从该路径复制到其他目录。
  3. 复制到该目录后。应该使用 tar -xvf filename 命令解压它
  4. 为该 ie ln -s test.tis test 创建一个软链接
  5. 结尾。

这可能吗 ?任何示例和详细的帮助都会很有用。

答案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."

相关内容