-1

Wants to get the access_token from the url below in bash script.

http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWfddfRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nsddssbWFuIn0.Sr3FT4EdssdsbddnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuYdsfsdfsdfsdfsd0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ&expires_in=3600&scope=all&state=3d417058-50a5-49&token_type=Bearer
Mohd. Shariq
  • 214
  • 3
  • 14

2 Answers2

1

With sed:

echo 'http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWfddfRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nsddssbWFuIn0.Sr3FT4EdssdsbddnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuYdsfsdfsdfsdfsd0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ' |  
sed 's/.*access_token=\([^&]*\).*/\1/'
MichalH
  • 1,062
  • 9
  • 20
-1

This should do it

URL="http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nbWFuIn0.Sr3FT4EnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuY0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ&expires_in=3600&scope=all&state=3d417058-50a5-49&token_type=Bearer"

token=$(echo $URL | cut -d '=' -f 2 | cut -d '&' -f 1)

echo $token

Mohd. Shariq
  • 214
  • 3
  • 14
  • [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Jul 20 '20 at 14:42
  • you wrap quotes for strings, the double quotes allow variable replacement while single quotes do not – Dark Blaze Jul 20 '20 at 14:46
  • Read the link. You are presuming that the resulting token contains no shell metacharacters, but this is not a safe assumption for inputs you do not control. – tripleee Jul 20 '20 at 16:05
  • Oh! ok, thanks for clarification :) – Dark Blaze Jul 23 '20 at 06:16