#!/bin/bash
# Function to show usageshow_usage(){echo"Usage: $0 [-y] package_name"exit1}# Check if at least one argument is providedif["$#" -lt 1];then show_usage
fi# Parse optionsDOWNLOAD_AND_INSTALL=falsewhilegetopts":y" opt;docase${opt} in
y )DOWNLOAD_AND_INSTALL=true;;\?) show_usage
;;esacdoneshift$((OPTIND -1))# Get the package namePACKAGE_NAME=$1# If no package name is provided, show usageif[ -z "$PACKAGE_NAME"];then show_usage
fi# Function to check if a command existscommand_exists(){command -v "$1" >/dev/null 2>&1}# Check if yum and yumdownloader are availableif ! command_exists yum || ! command_exists yumdownloader;thenecho"yum or yumdownloader command not found. Please make sure they are installed."exit1fi# Extract the base package nameBASE_NAME=$(echo"$PACKAGE_NAME"| cut -d. -f1)# If -y option is provided, download and install the packageif["$DOWNLOAD_AND_INSTALL"=true];thenecho"Downloading and installing package: $PACKAGE_NAME"cd ~ ||exit1 yumdownloader "$PACKAGE_NAME"RPM_FILE=$(ls "${BASE_NAME}"*.rpm 2>/dev/null)if[ -f "$RPM_FILE"];then rpm2cpio "$RPM_FILE"| cpio -idvm
rm -f "$RPM_FILE"echo"Package $PACKAGE_NAME installed successfully."elseecho"Failed to download package $PACKAGE_NAME."exit1fielse# Otherwise, list the package yum list "$PACKAGE_NAME"fi
使用方法:
查询能装的包:yum_i package_name
1
2
3
4
5
6
7
8
9
$ yum_i graphviz
Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos, subscription-manager
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
graphviz.i686 2.30.1-22.el7 base
graphviz.x86_64 2.30.1-22.el7 base
#!/bin/bash
# 检查是否提供了源目录和目标目录if[$# -ne 2];thenecho"Usage: $0 <source_directory> <destination_directory>"exit1fiSOURCE_DIR=$1DEST_DIR=$2# 检查源目录是否存在if[ ! -d "$SOURCE_DIR"];thenecho"Source directory does not exist."exit1fi# 检查目标目录是否存在,不存在则创建if[ ! -d "$DEST_DIR"];thenecho"Destination directory does not exist. Creating it now." mkdir -p "$DEST_DIR"fi# 创建软链接for file in "$SOURCE_DIR"/*;do ln -s "$file""$DEST_DIR"doneecho"Soft links created for all files in $SOURCE_DIR to $DEST_DIR"