#!/bin/bash # Function to display usage usage() { echo "Usage: $0 " echo "Converts the specified Bash script to a binary executable." echo "Example: $0 my_script.sh" exit 1 } # Function to install shc install_shc() { echo "Installing shc..." if command -v apt &> /dev/null; then sudo apt update && sudo apt install -y shc elif command -v yum &> /dev/null; then sudo yum install -y shc else echo "Error: Package manager not supported. Please install shc manually." exit 1 fi } # Function to fix line endings fix_line_endings() { echo "Fixing line endings for $1..." # Check if dos2unix is installed if command -v dos2unix &> /dev/null; then dos2unix "$1" else # If dos2unix is not available, use sed as an alternative sed -i 's/\r$//' "$1" fi } # Check the number of arguments if [ "$#" -ne 1 ]; then usage fi # Get the script path from the argument SCRIPT_PATH="$1" # Check if the specified script file exists if [ ! -f "$SCRIPT_PATH" ]; then echo "Error: File '$SCRIPT_PATH' does not exist." exit 1 fi # Check and fix line endings fix_line_endings "$SCRIPT_PATH" # Check if shc is installed if ! command -v shc &> /dev/null; then install_shc fi # Create a temporary directory for compilation TEMP_DIR=$(mktemp -d) # Compile the script using shc shc -f "$SCRIPT_PATH" -o "$TEMP_DIR/output_binary" # Check if compilation was successful if [ $? -ne 0 ]; then echo "Error: Failed to compile the script." rm -rf "$TEMP_DIR" exit 1 fi # Move the binary to the original script directory mv "$TEMP_DIR/output_binary" "$(dirname "$SCRIPT_PATH")/$(basename "$SCRIPT_PATH" .sh)_bin" # Cleanup rm -rf "$TEMP_DIR" # Success message echo "Success: Script '$SCRIPT_PATH' has been converted to binary as '$(basename "$SCRIPT_PATH" .sh)_bin'."