 Today, we are going to be looking at this, a USB credit card swiper. If you enjoy my tutorials and would like to see more, please think about contributing to my Patreon account at patreon.com forward slash metalx1000. Now this particular USB credit card swiper I got off Amazon for I think probably about $12 and it works as a hidden device. For those of you who don't know what a hidden device is, that's HID. It stands for Human Interface Device. Basically you plug this in and the computer detects it as a keyboard. So if your computer can accept the keyboard, this should work on it. And then, you know, whatever software you're using, which we're going to write something today in Bash. As long as it can take text input, you swipe the card and it outputs all the data that's on the strip. Now on cards, let me grab some here, like these three cards all have magnetic strips on the back. Yeah, there we go. These are hotel room card keys. I got them a little bit before I bought the card swiper. I was on a trip and I held on to these because they reprogrammed the doors after you leave so you keep these cards. They actually work as coupons too, these ones have pizza on them. Now credit card swipes, the strips on them, the magnetic strips are basically the same thing as an old cassette tape. In fact, if you look inside any credit card swiper, if you look in there, it actually looks just like the head of a tape recorder because it basically is. And what happens is you swipe this and basically on some credit card swipers, it just plays audio into an audio input and then the software decodes it. With this particular device, it's much easier than that. You don't need to decode it because the device itself decodes it and again just types it as a keyboard. I do want to note that this particular swiper, which I'll try to remember to put a link in the description to it on Amazon or you can just search USB card swipers on Amazon and it should come up, is that it is a three track, I think it's called, a three track card swiper, which these strips can have multiple sizes on them. So if you look at this, let me turn off my automatic focus here. You can see I've got two credit cards here and then one of the hotel room keys and you can see that the hotel room key strip is much bigger. I'm assuming if these are probably three, maybe this is a five track swiper, I'm not going to swipe that strip, I'm not sure on how many tracks. If I swipe this card, I kind of just get a end line character. I'm thinking that maybe the data is on those other two tracks and my card reader can't read it. But credit cards and my driver's license, which we'll also be looking at, will work. There we go. And so let's dive into the code. Okay, let's dive right in. Again, as always, I'm using Vim as my text editor but you can use whatever text editor you prefer. I'm going to call this script swipe.sh. And just while we're in here, what I'm going to do is I'm going to take a card and I'm going to swipe it. There we go. So this is the data from the card. And of course we can, with the credit card, it's pretty simple and straightforward. You can look at the card and see the card number and you can see that this is the credit card number right here. You can also see that this is obviously the last name then first name divided by a slash. And then right here is the expiration date, 2015, the month is five. And then there's other data here now and some other symbols, of course. If you just get out Google and Google magnetic card, credit card, swipe format or something to that effect, you'll get a bunch of sites telling you what the different characters and where everything is and you can do the same thing for driver's license and other cards as well because they're somewhat of a standard format. So here's an example of a website. Right here you can see this website and this is just one that comes up on Google. You have your length of this particular field and this one's one character. It's the percentage sign which basically says this is a track. Then you have the format code which is one character and if it's a B that indicates that it is a credit card or debit card. So of course you can see right up here we have the B meaning this is a credit card and we know that already. But if you want to write one code that can read multiple different formats of magnetic strips that would be an indicator to what portion, what function in your program if your warehouse functions would be. Then of course you got your credit card number and you got the delimiters which we're going to use to extract the information, the carrot symbols there and then the four digit expiration date and service codes and other stuff like that. So let's go ahead. I'm going to leave this on the screen here so that we can use it as a reference. But of course our first line of our code is, excuse me, bash or it's our shebang line telling our operating system. This code is a bash script and that you should use the bash interpreter. I personally like to clear the screen at the beginning of my codes. I think it looks nice. And then we're going to say welcome, a little welcome message for your program. Now we want to be able to swipe the card, get the information, display it and then not have the user have to start the program over again so we're going to put everything in a loop so that the program continues until we choose to exit or it's killed by the end user which we will put in an exit feature. But for now we're just going to say while one, which is how I do it, different people do it, different things, different ways, one just indicates true so it will be true and we'll continue to loop until it's killed. We're going to say, please swipe your card. And then we're going to use the read function again. This is, I'm assuming you know the basics of shell scripts. I have lots of tutorials on echo and while loops and clearing the screen and reading data from the user and all that sort of stuff. And variables, we're going to be using the cut command a lot in here to divide up variables. I've gone all over that in previous tutorials. I recommend watching my previous tutorials. This one is just more focused on how to cut the information for this particular task at hand which is reading a credit card. I don't want those quotation marks there. So we're going to use read which waits for the user to type some input and again this credit card swiper is a hidden device so it's no different than using a keyboard. So we're going to say read what is typed and save it to a variable called data. At this point I'm going to clear the screen again. We're going to create some variables here and one of the things we want is the credit card number. So what we're going to do is we're going to take the variable of data and we're going to cut it up. Now this could be done with some bash internal commands but I'm just very used to using the cut command so that's what I'm going to use. Of course if you're more at using more better, more better, if you're better than me at using the internal bash functionality you can do this without the external command of cut but cut's going to be on pretty much every system that you're going to be working with pretty much, pretty much. So what we're going to do here is we're going to say dollar sign and then parentheses which is saying the variable that we're creating which is num for number is going to be the output of whatever commands we put inside these parentheses. And then we're going to say data which is the full string basically in our case right here. And we want to find everything after this B and before this carrot symbol. So we're going to say cut with a delimiter of B and we're going to say field 2. So that's a field 1 is whatever is before the B, field 2 is everything after the B. And then we're going to take that since at this point num is everything past the B. We want to cut that to everything before this carrot symbol so we're going to cut it again. We're going to say with a delimiter of the carrot symbol field 1. Next we're going to say name and here we're going to grab the full name and then we'll divide that up for the first name and last name after that. So we're going to say echo again the dollar sign data for the variable of data and colors don't look right. Oh because I didn't put the dollar sign here. I was like that's not right. Okay. So we're going to say cut with a delimiter of and the name as you can see is between the two carrot symbols. So if we're dividing up we're using the delimiter of the carrot symbol we've got three fields. Field 1, field 2 and field 3. And of course we want field 2 in this case so dash F2. So that will get us the full name which right now the variable name is Smith slash John. So what we're going to say is we want the last name to equal whatever name is. We're going to pipe that into the cut command and this time we're going to use the delimiter of a forward slash and this is the last name. The last name comes first in this case. So we're going to say F1. So basically name is this. We're saying take that everything before that first slash. Next let's just cheat and copy and paste that and we will say F name for first name and it's the same thing but it's everything after that forward slash and this should actually be like that. There we go. Now the expiration date. The expiration date is going to be a little bit different. We're going to use the cut command to get everything after the second carrot but there's no real special delimiter. We don't know if this is always going to be a 1. And there's also a 1 in the thing so it's not a good way to divide up. So we're going to grab first off everything this after this second carrot and put that into a variable called X date. And then we're going to take the expiration date which is all this and just say grab the first two characters for the year and then grab characters three and four for the month because the date will always be a four character in size. So say X date equals and we'll say dollar sign echo. I'm sorry dollar sign and inside parentheses echo. And we'll say dollar sign data and we'll pipe that into the cut command with the delimiter of and don't forget I put that in the wrong spot. There we go. Of the carrot symbol and we want field three. So again right now expiration date equals all that. So now we're going to say and again I've done over all this in previous tutorials I want to make that clear. I'm assuming you know the basics but I'm going to try to explain things. If you don't want parentheses sign we're not writing command. We're taking a variable and we're saying take the variable already exists of X date and start very beginning zero and go to characters. That would be our year and then we're going to say forward slash. So it's just going to print the forward slash there dollar sign and then our braces and we're going to say X date and we're going to say go to the end of the second character and go to characters and print that. Okay so if we typed everything correctly now we can output our data. I'm going to delete that line because we're not going to need that. That was just for our reference while creating those variables and now for output we got our input we divided it up put it all into variables. Now we're going to say echo card number equals the variable num and then we'll say echo card holder dollar sign F name dollar sign L name so it goes first name last name and then we're going to say echo expiration date colon dollar sign X date and then just for formatting we're going to say echo and we'll put a little dividing line here because at this point it's going to loop over and ask you to swipe the card again. Now hopefully we've typed everything correctly we're going to save that. Now we just created the scripts we have to make it executable change mod plus X and the name of our script and then dot slash the name of our script and at this point it's going to ask us to swipe the card so let me grab a card let me grab the swiper here and we're going to swipe and I typed something wrong the letter must be a single character I wish you told me which line but shouldn't be hard to figure out oh right here I put a space I'm betting that is it so we'll run our code again and there we go so it's grabbed the credit card number I displayed it there card holder John Smith expiration date 15 if I swipe the card again well you don't really see anything different because it typed out the same stuff so the output is the same because it clears the screen and then looped over again now at this point for us to exit the program we have to kill it with control C not a problem we want to make it a little bit nicer for the end user so what we're going to do here is go back into our code and we're going to create a if then statement that checks the data that is swiped or typed in really if we're going to check data so if the data that is inputted equals an empty string there's other ways to do this I'm just going to say if the data is empty well then we're going to echo exiting and then we can say exit zero which will completely exit the script now we can turn this into an if then and then we can say else and do the rest it doesn't really matter because we're going to be exiting at the script here if that is true and if not it's going to run the script so you could do an if else for the rest of this or you can just it doesn't really matter in this particular situation so let's go ahead and run our code again so we'll run it and of course we can swipe and it brings up the information but if we hit enter it exits out so let's go back in here and put a message to the end user saying press enter to exit so that they know so you run the code at this point I can hit enter and it exits or I can swipe the card see it and at this point I can enter and exit so you could also make it so if they type exit or type Q for quit or whatever you want but I think just hitting enter because you're either going to swipe a card or hit enter to exit so again I hope this code I hope remember to put this code link in the description to it it should be up on my pay spin site which is linked to on my website filmsbychrist.com check out filmsbychrist.com I appreciate you watching so as always I hope that you enjoy this tutorial again if you like my videos and want to support my videos you can check out my patreon page it's patreon.com forward slash metalx1000 there should be an annotation on the screen right now as well as a link in the description and there you can help support me on a monthly basis you can choose to donate anywhere from $5 on up and there are different rewards depending on how much you support me with and also there are certain levels where you start getting to have more input towards me on what you want to see so if there's other topics you like that you want to see that I've gone over you want me to continue a good way to get that done is to become one of my patrons on Patreon and start supporting me and you'll have more input on those different levels certain levels you get to vote on what topics I'm going to go over and of course you have more communication with me so I can hear more of what you want so as always thank you for those who are supporting me and I hope that you continue to support me I hope that I can continue doing videos so thank you again for watching please visit filmsbychrist.com that's Chris with the K there should be a link in the description and I hope that you have a great day