Jump to content

Bash Script


PixL
 Share

Recommended Posts

I have this bash script which functions but I am confused as my output appears as below with the stdout directly BEFORE my next bash prompt.  What formatting do I need to change to fix this?

#!/bin/bash
function hex2string () {
  I=0
  while [ $I -lt ${#1} ];
  do
    echo -en "\x"${1:$I:2}
    let "I += 2"
  done
}
read -p 'Hexstring: ' varname
hex2string "$varname"
me@unknown ~ $ ./hex2ascii 
Hexstring: 4e4554474541523437
NETGEAR47me@unknown ~ $ 

 

Link to comment
Share on other sites

You use the '-n' flag in your echo to keep all the characters on one line during your loop. This means the last character to be decoded does not contain the newline character, either. You should print a newline after the loop has finished inside of the function with a simple 'echo'.

Quote
-n      do not output the trailing newline

https://linux.die.net/man/1/echo

Link to comment
Share on other sites

Thanks guys,

I fixed it in the end and added a few extra bits... it takes a .16800 pmkid file and prints out the ssid of each separate pmkid.

#!/bin/bash
function hex2string () {
  I=0
  while [ $I -lt ${#1} ];
  do
    echo -en "\x"${1:$I:2}
    let "I += 2"
  done
}

read -p 'Enter PMKID filename: ' filename
while read -r pmkid; do
ssid="${pmkid:59:500}"
hex2string "$ssid"
echo
echo "$pmkid"
done < $filename

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...