Bash脚本安装Tomcat后重命名目录失败,求解决方法
Fixing Tomcat Installation Script Rename & Cleanup Issues
Let's break down why your rename and tar removal steps aren't working, then fix the script step by step:
1. Key Problems in Your Original Script
- Broken
mvcommand: Your currentmv $TOMCAT_FILE/apache-tomcat-8.5.47has two critical issues:$TOMCAT_FILEpoints to the tar archive itself (/sys/project/apache-tomcat-8.5.47.tar.gz), not the extracted directory. The unpacked folder lives at/sys/project/apache-tomcat-8.5.47after runningtar zxf.- The
mvcommand requires a target destination—you're only specifying the source folder without telling it what to rename/move it to.
- Incorrect
rmpath:rm -rf $TOMCAT_FILE/apache-tomcat-8.5.47.tar.gztries to delete a file inside the tar package (which doesn't exist), but$TOMCAT_FILEis already the full path to the tar file itself.
2. Corrected Script
TOMCAT_FILE=/sys/project/apache-tomcat-8.5.47.tar.gz # Define clear paths for readability and maintainability EXTRACTED_DIR=/sys/project/apache-tomcat-8.5.47 TARGET_DIR=/sys/project/apache if [ -e "$TOMCAT_FILE" ] then echo "##############" echo "" echo "Installing Tomcat" tar zxf "$TOMCAT_FILE" -C /sys/project/ echo "##############" echo "Tomcat extraction completed" # Rename the extracted directory only if it exists if [ -d "$EXTRACTED_DIR" ]; then mv "$EXTRACTED_DIR" "$TARGET_DIR" echo "Tomcat directory renamed to 'apache'" else echo "Error: Extracted directory $EXTRACTED_DIR not found!" exit 1 fi # Remove the original tar file (no need for -rf on a single file) rm -f "$TOMCAT_FILE" echo "Tomcat tar file removed" fi
3. What Changed & Why
- Added explicit path variables:
EXTRACTED_DIRandTARGET_DIReliminate path typos and make the script easier to adjust for different Tomcat versions. - Fixed
mvcommand: Now it correctly targets the unpacked directory and renames it to your desiredapachefolder. - Fixed
rmcommand: Uses$TOMCAT_FILEdirectly (since that's the full path to the tar) and uses-finstead of-rf(recursive delete isn't needed for a single file). - Added error checking: We verify the extracted directory exists before trying to rename it, so you'll get a clear message if extraction fails instead of silent failure.
4. Post-Run Verification
After executing the corrected script, check /sys/project/—you should see the apache directory (instead of the original apache-tomcat-8.5.47) and the tar file should no longer be present.
内容的提问来源于stack exchange,提问作者Anji
相关产品推荐
相关产品推荐

