0

I have a problem to adding a second case. I write this little script to login on pc and executing a command on them. But I'm having trouble defining a shortcut to the command. it should look like this, for example:

./script.sh 44 - login on this pc via ssh (this works)

./script.sh 44 r - reboot this pc via ssh (this don't works because i have a problems with defining a second case)

so for the test I wrote it:

LOG="$LOG""$AUT""$NMBR""$NR""$DOMAIN" "$Command"
        $LOG

and define a variable

$Command
Command="sudo reboot"

but this command doesn't execute on pc what wrong i do ?

I put the entire script below.

DOMAIN="domain.domain.com"
PASS1="pass"
AUT="auth"
NMBR="001010"
LOG="sshpass -p$PASS1 ssh user@"
Command="sqlite3 /bin/ade/activity.sqlite 'Delete from state'"

if [[ $# == 0 ]]; then
    echo "Invalid script call, give location or number as parameter:"
    echo "2  / plau -    NR 2 / Pn | I don't think it exists ?"
    echo "44 / neun -    NR 00101044 / Neundorf"
    echo "45 / aks -     NR 00101045 / Anton-Kraus-Straße"
    echo "46 / chamiss - NR 00101046 / Chamissostraße"
    echo "47 / prei -    NR 00101047 / Preißelpöhl"
    echo "48 / sud -     NR 00101048 / Südvorstadt"
    echo "49 / capitol - NR 00101049 / Capitol"
    echo "50 / kgs -     NR 00101050 / Dr.-Karl-Gelbke-Straße"
    echo "51 / reusa -   NR 00101051 / Reusa"
    echo "52 / haus -    NR 00101052 / Seehaus"
    echo "53 / bus -     NR 00101053 / Busbahnhof"
    echo "54 / kn -      NR 00101054 / Knielohstraße"
    echo "55 / seume -   NR 00101055 / Seumestraße"
    echo "56 / neuels -  NR 00101056 / Neue Elsterbrücke"
    echo "57 / haupt -   NR 00101057 / Hauptfriedhof"
    echo "58 / tunnel2 - NR 00101058 / Tunnel II (Stadtgalerie)"
    echo "59 / hlp -     NR 00101059 / Hans-Löwel-Platz"
    echo "60 / tunnel4 - NR 00101060 / Tunnel iV (PSB-Service)"
    exit 1;
    
else

    case  in
        "44") NR="0001"; LOGY="sshpass -p$PASS1 ssh mtvm@$AUT$NR$DOMAIN" ;;
        "45") NR="45" ;;
        "46") NR="46" ;;
        "47") NR="47" ;;
        "48") NR="48" ;;
        "49") NR="49" ;;
        "50") NR="50" ;;
        "51") NR="51" ;;
        "52") NR="52" ;;
        "53") NR="53" ;;
        "54") NR="54" ;;
        "55") NR="55" ;;
        "56") NR="56" ;;
        "57") NR="57" ;;
        "58") NR="58" ;;
        "59") NR="59" ;;
        "60") NR="60" ;;
        
        "neun") NR="0001"; LOG2="sshpass -p$PASS1 ssh mtvm@$AUT$NR$DOMAIN" ;;
        "aks") NR="45" ;;
        "chamiss") NR="46" ;;
        "prei") NR="47" ;;
        "sud") NR="48" ;;
        "capitol") NR="49" ;;
        "kgs") NR="50" ;;
        "reusa") NR="51" ;;
        "haus") NR="52" ;;
        "bus") NR="53" ;;
        "kn") NR="54" ;;
        "seume") NR="55" ;;
        "neuels") NR="56" ;;
        "haupt") NR="57" ;;
        "tunnel2") NR="58" ;;
        "hlp") NR="59" ;;
        "tunnel4") NR="60" ;;
        *) echo "Incorrect parameter"; exit 1 ;;
        
        esac

            echo "Login on $LOG$AUT$NMBR$NR$DOMAIN"
            
        LOG="$LOG""$AUT""$NMBR""$NR""$DOMAIN" "$Command"
        $LOG
        
        LOG2="$LOG2"
        $LOGY
        
fi
2
  • 3
    Not exactly sure what you're trying to do, but you should probably take a look at How can we run a command stored in a variable?. When you say " i have a problems with defining a second case" and "this command doesn't execute on pc", it's often useful to show (1) what you expected, and (2) what happens instead. Including any errors.
    – ilkkachu
    Jan 24, 2022 at 9:41
  • 1
    Your case statement is missing a variable: it should be case $var in . Also, why are you adding a separate case for each NR instead of just saying NR=$1?s
    – terdon
    Jan 24, 2022 at 9:53

1 Answer 1

0

This is confusing to read. Why not show us where you get into trouble? As it is, this script is a missing value between case in, and you have two lines with LOG suggesting you are going to do two ssh logins in sequence.

This line won't work either, the assiged value should be quoted around the space because as-is it will make Bash think $Command should be executed with LOG=... as local env.

LOG="$LOG""$AUT""$NMBR""$NR""$DOMAIN" "$Command"

should probably be

LOG="$LOG$AUT$NMBR$NR$DOMAIN $Command"

and rather than redefining LOG confusingly, just do:

$LOG$AUT$NMBR$NR$DOMAIN $Command

I would suggest removing all the LOG* vars altogether though, this is not very readable and why let someone configure what command gets called? Both LOGY and LOG2 are only set for host NR 0001, but the first only if argument is numeral and the other if given as string identifier. Is it just the user that is different for that host? Then why not use user=mtvm there.

At any rate, I think this is more or less what I would write if it had to be in one script. I've added a 'sudo reboot' with another case/esac:

# ... declare vars PASS1, DOMAIN, etc.
user=$(whoami)

# your argument checks and help output here

case $1 in
  neun ) shift; set -- 0001 "$@" ;;
  # other cases
esac

case $1 in
  0001 ) NR="$1"; user=mtvm ;;
  # other cases
esac

if [ $# -eq 2 ]; then
  case "$2" in r ) command="sudo reboot" ;; esac
fi

if [ -n "${command-}" ]
then
  sshpass -p$PASS1 ssh $user@$AUT$NR$DOMAIN $command
else
  sshpass -p$PASS1 ssh $user@$AUT$NR$DOMAIN
fi

Assuming the hostname is aut0001domain.domain.com which seems wrong, but that is what this does.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .