grep access_token in bash script from the URL [duplicate] - bash

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
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

With sed:
echo 'http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWfddfRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nsddssbWFuIn0.Sr3FT4EdssdsbddnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuYdsfsdfsdfsdfsd0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ' |
sed 's/.*access_token=\([^&]*\).*/\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

Related

Can't assign linux command output into a variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I've tried to write the following code in bash but I am not able to get the output to be echoed.
part1="blkid | grep -P 'CENTOS 7' | cut -c1-9"
echo "$part1"
Try this
part1=$(blkid | grep -P 'CENTOS 7' | cut -c1-9)
echo "$part1"

bash: trouble getting file size from remote url [duplicate]

This question already has answers here:
How to get remote file size from a shell script?
(14 answers)
How to remove a newline from a string in Bash
(11 answers)
Closed 4 years ago.
I am trying to get file size of remote url in the following manner:
I am trying the below command:
curl -sI $url | grep -i content-length | cut -d ' ' -f 2
Its giving 17857797\C-M
I dont understand why \C-M is adding
Append | dos2unix or | tr -d '\r' to remove carriage-return.

Get file extension with bash script [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
i' m trying to extract the file extension in bash without using regex.
i' ve tried the following
extension = $(echo $1 | cut -f 2 -d '.')
extension is the variable
$1 contains something like: file.txt or file.pdf etc.
this code is outputting:
./prova.sh: 3: ./prova.sh: extension: not found
extension=$(echo sample.txt | cut -f 2 -d '.')
echo $extension
Above command will give you the output as expected txt

Get substring of string in korn shell [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 6 years ago.
Have a string as xxxxxxx.txt
Need to get the only xxxxxxx string without extension.
How to get this using korn shell?
s=abcdef.txt
s_base=${s%.txt}
...will assign abcdef to s_base.
STRING=xxxxxxxxxx.txt
echo $STRING | rev | cut -f 2- -d "." | rev

How to extract path to filename [duplicate]

This question already has answers here:
How to extract directory path from file path?
(9 answers)
Closed 6 years ago.
In bash programming If VAR="/home/somestuff/work/OutExample/Air",
How to get "workOutExampleAir" ?
Thanks
cut -f4- -d/ --output-delimiter "" <<< "$var"
cut utility takes field from 4 with delimiter /
in bash
name=$(cut -f4- -d/ --output-delimiter "" <<< "$var")
echo "$name"

Resources