|
Article:
PHP in the Command Line by: Robert Plank There's a single line you can add to your web host's control panel that will automatically archive your content. LISTEN CLOSELY AND YOU'LL HEAR THE OCEAN Ever run commands in DOS? You've used a shell. A 'shell' in the computer world is a place where you enter commands and run files by name rather than clicking around different windows. Most web hosts let you operate a shell remotely. This means that you can type commands in window on your computer, that are actually run on your web host, thousands of miles away. I'd like you to log in to your shell now. If you can't do it by going in to DOS and typing 'telnet your.domain.here', your web host probably uses 'SSH' -- a secure shell. You'll have to ask your host how you can log in to the shell, they might tell you to download a program called 'PuTTY' and give instructions how to use it. If you can't login to your shell, or aren't allowed, you'll just have to sit back and watch what I do. Now that you're logged in, type: echo hi On the next line will be printed hi Try this: date +%Y This prints the current year. That's 2004 for me. So what if we combined the two? Try: echo date +%Y Well, that doesn't work, because the computer thinks you're trying to echo the TEXT 'date +%Y' instead of the actual COMMAND. What we have to do here is surround that text in what are called 'back quotes'. Unix will evaluate everything enclosed in back quotes (by evaluate, I mean it'll treat that text as if it were entered as a command.) Your back quotes key should be located on the upper-left corner of your keyboard, under the Esc button. PIPE DOWN, OVER THERE... Type this in: echo `date +%Y` Gives us '2004'. You could even do something like this: echo `dir` Which puts the directory listing all on one line. But now, we put our newfound knowledge to good use. Unix has another neat feature called piping, which means 'take everything you would normally output to the screen here, and shove it whatever file I tell you to.' So say I had something like this: echo 'hey' > test.txt Now type 'dir' and you'll see a new file, test.txt, that wasn't there before. View it off the web, or FTP it to your computer, do whatever you have to, to read the file. It should contain the word 'hey'. Likewise, dir > test.txt would store the directory listing into 'test.txt'. HERE TODAY, GONE TOMORROW But say we wanted that text file to be named according to the current date. You already have the pieces to figure all that out, if you think about it. Type: date --help to get a listing of all the possible ways to represent the date. The ones you want to represent the year, month and day are %Y, %m, and %d (capitalization *is* important here). This is what you want: echo `date +%Y%m%d.html` Running this today, January 8th, '2004
|