Search code examples
hostnamelftp

Read hostname and write it to another script at specefic position?


I wanna make startup script that change 2 lines in another script at startup or maybe not another script but to modify current to do same. Here my current script that i use for ftp mirror.

User name must me my hostname and that way i dont need to modify this script for every new device but only change hostname of my devices. Can someone plz help me achieve this?

#!/bin/bash
login="username"
pass="pass.username"
host="10.10.10.12"

base_name="$(basename "$0")"
lock_file="/home/mit/$base_name.lock"
trap "rm -f $lock_file" SIGINT SIGTERM
if [ -e "$lock_file" ]
then
    echo "$base_name is running already."
    exit
else
    touch "$lock_file"
    lftp -u $login,$pass $host << EOF
    mirror --exclude-glob lost+found/ -n -e --use-cache  /music/ /music/
    quit
EOF
    rm -f "$lock_file"
    trap - SIGINT SIGTERM
    exit
fi

Solution

  • I find a solution for my problem. Here script that work way i want.

    #!/bin/bash
    sitecode=`cat /etc/hostname`;
    login="${sitecode}"
    pass="pass.${sitecode}"
    host="10.10.10.12"
    
    base_name="$(basename "$0")"
    lock_file="/home/mit/$base_name.lock"
    trap "rm -f $lock_file" SIGINT SIGTERM
    if [ -e "$lock_file" ]
    then
        echo "$base_name is running already."
        exit
    else
        touch "$lock_file"
        lftp -u $login,$pass $host << EOF
        mirror --exclude-glob lost+found/ -n -e --use-cache  /music/ /music/
        quit
    EOF
        rm -f "$lock_file"
        trap - SIGINT SIGTERM
        exit
    fi