How can a shell script get the number of an incoming/outgoing call? new user here from Montréal, Canada!

So i have a shell script that runs when a call comes in, and another one that runs when i make a call. How can this script get the phone number of the caller / of the number being called? Grepping through the logcat output doesn't show it.
Receiving caller number from incoming call

Tasker maintains a global variable  named Call Number (%CNUM) which would reflect caller number for the incoming call. 

If you intend to run your script when the device rings, you may setup the following profile:


Profile: Event &rarr; Phone &rarr; Phone Ringing &rarr; Priority: Highest
Task: (Actions): Code &rarr; Run Shell &rarr; Command: sh FILE_PATH "%CNUM"

Replace FILE_PATH with the absolute file path of your script. In your script, %CNUM can be received as an argument by storing $1 into a variable. (See this answer by Bruce Ediger for any related help.)

Example script:

#!/system/bin/sh
incoming_no="$1"
echo "$incoming_no" &gt;&gt; /sdcard/incoming_no.txt


Save the script and try executing it from an action in that profile. For an incoming call, it would create or append the file incoming_no.txt with the caller number.


Note: Tasker supports creating global variables, so if you don't intend to run your script with that profile, use the action Variable &rarr; Variable Set to store the value of %CNUM into a user specific global variable. Whenever time is ripe, use that variable in your script from Tasker.

Receiving called number from outgoing call

Tasker doesn't have an inbuilt support for this, as it seems to me. Worry not, since this answer by sush can be used in Tasker for our goal. The crux of that answer is: listen to a broadcast with action android.intent.action.NEW_OUTGOING_CALL and receive the value from a particular extra type key.

Tasker has support for receiving intents. Before we begin with profile, hear me out on how I extracted the value. 

I made a call to a number and immediately ran the following command (requires adb in PC):

adb shell dumpsys activity


In the output, under the section ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts) I noticed:


BroadcastRecord{32699300 u0 android.intent.action.NEW_OUTGOING_CALL}
act=android.intent.action.NEW_OUTGOING_CALL flg=0x10000030 (has extras)
extras: Bundle[{android.intent.extra.PHONE_NUMBER=+919XXXXYYYZZ}]


Here, android.intent.extra.PHONE_NUMBER is the extra type key. 

As for Tasker, setup your profile in the following fashion:


Profile: Event &rarr; System &rarr; Intent Received:


Action: android.intent.action.NEW_OUTGOING_CALL
Priority: Highest

Task: (Actions): 
the number can be retrieved from the variable 

%android_intent_extra_phone_number 


As usual, store the value of that variable into a user specific global variable using Variable &rarr; Variable Set and use the latter in your script whenever you want.


Note: 


The solution is tested on Android 4.2.1 and Android 5.0.2 (COS12).
Per userguide of Tasker, the Variables tab is not shown if Menu / Prefs / UI / Beginner Mode is enabled.
Caller number for incoming calls can alternatively be retrieved by listening the broadcast intent with action android.intent.action.PHONE_STATE and receiving the number from %incoming_number. Use %state in a condition to check whether its value is RINGING or IDLE. If former, get the value from %incoming_number.