Bash check if file exists on startup

后端 未结 1 455
夕颜
夕颜 2021-01-17 07:53

I am trying to run a BASH script on a Debian machine. The script should run on every startup (so I put the .sh file in /etc/init.d) if the mac addr

相关标签:
1条回答
  • 2021-01-17 08:40

    Slightly modified version:

    #!/bin/bash
    macfile='/root/.mac.txt'
    mac=$(/sbin/ifconfig | grep 'eth0' | tr -s ' ' | cut -d ' ' -f5)
    
    # Shut down if file does not exist
    if [ ! -f $macfile ]; then
        shutdown -h now
    fi
    
    # Verify MAC address against cached value
    output=$(cat $macfile)
    if [ "$mac" = "$output" ]; then
        echo "Server will start" 
    else 
        shutdown -h now 
    fi 
    

    Explanation:

    • Test for the existence of the file before you read from it
    • The "[" and "]" characters must have a whitespace before and after them
    • When running sub-commands, use the $( ... ) syntax instead of backticks
    0 讨论(0)
提交回复
热议问题