Sunteți pe pagina 1din 6

What is Qshell: Technical Examples By Buck Calabro, Lead Programmer Analyst

Let's see qsh at work. Start an iSeries workstation session, go to a command line and type qsh. Press Enter. You should see a command line with a '$' prompt at the top of the page and an area to type at the bottom. If you got an error message, perhaps you haven't installed OS/400 option 30 yet. Let's see if you have a home directory set up. Type pwd (print working directory) and press enter. qsh echoes your command, preceding it with a '>', puts the results of the command underneath it, and finally shows the '$' prompt, indicating that qsh is ready for more commands. (For long running commands, the keyboard remains unlocked, but no '$' prompt will appear until qsh is ready to process the next command.) In my case, I see: > pwd /home/buck $ ===>______________ If yours says > pwd / $ ===>______________ then your home directory isn't set in your user profile. First, let's see if you have a home directory: > ls -al /home total: 492 kilobytes drwxrwsrwx 9 QSYS 0 8192 Feb 17 2005 . drwxrwsr-x 62 QSYS 0 385024 Feb 6 14:54 .. drwxrwsrwx 2 BUCK 0 8192 Feb 17 2005 buck $ ===>________________ This command means 'list files within the /home directory and show me entries starting with a dot (. and ..) and show me a long (detailed) format. The two directories starting with a dot are special, and exist in every directory. '.' expands to 'this directory' and '..' expands to 'the directory immediately higher than his one.' Let's say your name/profile isn't in here yet. You can easily create your home directory: > mkdir /home/example $ ===>__________ Use the ls command to verify that your directory is created. Now, change your user profile to point to the directory you just created. You can go to an iSeries command line within qsh by pressing F21. Then chgusrprf my_profile homedir('/home/example') [NOTE: you'll need security officer authority to do this.] Exit qsh by pressing F3, then start it again (so you start in your home directory.) Use the pwd command to verify that you are in /home/example. Now, by default, everything you do will end up in this directory.

Another thing qsh uses is environment variables. Various utilities, programs and commands can store information in these variables for use any time during your session. You can see all your environment variables with the printenv command. Try it now. You'll see that (generally) there are more variables than can fit on a single display. Use the Page Up and Down keys to see all the output. One of the most valuable variables is PATH. We can use another command to display the contents of the PATH variable. echo $PATH shows me: > echo $PATH /usr/bin:/usr/apache-ant-1.6.5/bin $ ===>__________ this may differ in your case. The '$' tells qsh to use the value of the variable that immediately follows, in this case, PATH. echo is a general purpose command that you can use in a script to show the status of a process, or give help text, or anything you like. Try this as an example: echo The current path is $PATH Environment variables can be changed in qsh, too. In my case, I really want to add the current directory (not just my home directory!) to the path so that qsh will find commands in the directory I'm currently in. Remember, you can use pwd to see where you are. I can add 'this directory' to the front of the path like this: export PATH=.:$PATH Do note that case is important in qsh! PATH is not the same as Path or path. Look at your new path with echo. Having the current directory in your path is a great idea, but it's a bit of a pain to manually add it every time you start qsh. qsh will automatically run a script for you when it starts. Actually, it'll run several scripts for you! In order, it looks for the following, and runs them if it finds them: 1) /etc/profile -- sets variables for everyone who uses qsh. 2) $HOME/.profile -- sets variables for you. Note that $HOME expands to your home directory. Try echo $HOME and see what appears. Finally, after running either or both of these, an environment variable named ENV might have been set. ENV can point to another script file to be run, and if it's present, that's exactly what happens. Here's my /etc/profile: export ANT_HOME=/usr/apache-ant-1.6.5 export JAVA_HOME=/QIBM/ProdData/Java400/jdk14 export AXIS_HOME=/usr/axis-1_3 export AXIS_LIB=$AXIS_HOME/lib export AXISCLASSPATH=$AXIS_LIB/axis.jar: (clipped) export XALAN_HOME=/usr/xalan-j_2_7_0 export XALANCLASSPATH=$XALAN_HOME/xml-apis.jar: (clipped) export XMLSEC_HOME=/usr/xml-security-1_3_0 export XMLSEC_LIB=$XMLSEC_HOME/libs export XMLSECCLASSPATH=$XMLSEC_LIB/xmlsec-1.3.0.jar export PATH=.:/usr/bin:$ANT_HOME/bin export CLASSPATH=.:/QIBM/ProdData/HTTP/Public/jt400/lib: (clipped) export NLSPATH='/QIBM/ProdData/OS400/Shell/MRI2924/%N' export QSH_USE_PRESTART_JOBS=Y You can see that I create environment variables for Axis, Java, Xalan and XMLSEC. That's because I'm a developer, and it's far easier for me to do ls -al $JAVA_HOME/bin than it is to type ls -al /QIBM/ProdData/Java400/jdk14 /bin. You'll find that $JAVA_HOME is used on most platforms -- try a Windows command prompt (on a Windows machine with Java installed!) echo %JAVA_HOME% and see what comes up.

I also use $HOME/.profile to store things that should only affect me as a developer (not necessarily every user who uses qsh.) export CLASSPATH=$HOME:$CLASSPATH export PATH=$HOME:$PATH You can edit these files in a variety of ways. If you are PC/GUI-centric, use iSeries Navigator to share your home directory and then map a Windows drive letter to that share. Then you can use your favourite PC editor along with Windows Explorer to edit and view files in your iSeries home directory. If you're green-screen-centric, use the iSeries command edtf. From within qsh, press F21 to get an iSeries command line and then edtf '/etc/profile'. This is a pretty simple editor -simpler even than SEU. It's pretty self-explanatory, except for the 'save' key which is F2. At the very least, make sure that PATH has the . (dot or current) directory in it. Let's see an example of a shell script. Of course, it isn't particularly useful, but it will run on any system! edtf '/home/buck/buckscript' echo Now running ${0} date echo Number of parameters specified ${#} # see if any parameters were entered if test ${#} -gt 0 then # print all parameters at once echo Parameters entered: ${*} # spin through the parameters and print each one separately for parm in ${*}; do echo ${parm} done fi Run your script using the name you used. In my case, it's: > buckscript Now running /home/buck/buckscript Thu Feb 9 13:15:16 Eastern Standard Time 2006 Number of parameters specified 0 $ ===>________________ If we try it with several parameters, here's what we get: > buckscript a b c d e Now running /home/buck/buckscript Thu Feb 9 13:17:15 Eastern Standard Time 2006 Number of parameters specified 5 Parameters entered: a b c d e a b c d e $ ===>__________ Let's spend a moment looking at a very useful command called grep. It scans a file and prints out lines matching a pattern called a regular expression. Regular expressions are the topic of entire books, and I suggest you use your favourite search engine to look up some very good information on them. Another common term for regular expression is regexp. We already have a file to play with: the above shell script. Suppose we had done some very good comments (the

lines beginning with #) and that those comments would serve quite well as an outline of the script. Why don't we print out only the comments? Here's our first go: > grep \# buckscript echo Number of parameters specified ${#} # see if any parameters were entered if test ${#} -gt 0 # print all parameters at once # spin through the parameters and print each one separately $ We needed to specify the \ before the character we're looking for (#) because # is treated in a special way by qsh. Anyway, it's not quite right. We picked up lines with a pound sign in the middle. What we need is to find lines where the pound sign is at the beginning of the line. There's a regexp for that: ^. Here's our second go: > grep ^\# buckscript # see if any parameters were entered $ Well it did what we said: only lines where the pound sign is the first character. Classic example of "don't give me what I asked for -- give me what I want!" What I want is any line having optional white space followed by a pound sign. Try this: > grep ^[[:space:]]*\# buckscript # see if any parameters were entered # print all parameters at once # spin through the parameters and print each one separately $ There we are! In order, the regexp pattern means: ^ -- pattern begins at the start of the line. [[:space:]] -- match any white space, including tab. * -- match any number of white spaces (including none). \ -- treat the next character as a literal. Don't do the normal qsh function with it. # -- match a pound sign. This regexp will print any line where the first non-blank character is a pound sign. Let's work with a few more commands. I have a very contrived example to list text files containing the phrase 'print' (in any case). > ls | xargs grep -i 'print' BuckC.java: System.out.println("java.ext.dirs=" + BuckC.java: System.out.println("java.home=" + (clipped) BuckC.java: System.out.println("sun.boot.class.path=" + BuckC.java: System.out.println("java.class.path=" + BuckC.java: System.out.println("java.vm.version=" + BuckC.java: System.out.println("java.version=" + buckscript: # print all parameters at once buckscript: # spin through the parameters and print each (clipped) $ This takes the output of the ls (list directory) command and pipes it (with the vertical bar "|") into xargs. xargs takes standard input and turns it into parameters (arguments) for grep, whose -i switch says to ignore case when searching. Basically, ls creates a list of files, and grep reads them one by one. Here's one more useful utility: sed. sed is the Stream Editor. You can use it to automate changes to a stream file. Fortunately, everything in the root file system is a stream file, including standard input, standard output and standard error. Let's pretend we're moving our shell script

example to another machine, and that machine requires comments to be prefixed with "#!" instead of "#". Here's a sed example that'll make that change: > sed -e 's/\#/\#\!/g' buckscript echo Now running ${0} date echo Number of parameters specified ${#!} #! see if any parameters were entered if test ${#!} -gt 0 then #! print all parameters at once echo Parameters entered: ${*} #! spin through the parameters and print each one separately for parm in ${*}; do echo ${parm} done fi $ sed sends its output to standard output by default. You could redirect it to a file (with ">") if you wish: > sed -e 's/\#/\#\!/g' buckscript > newscript $ > cat newscript echo Now running ${0} date echo Number of parameters specified ${#!} #! see if any parameters were entered if test ${#!} -gt 0 then #! print all parameters at once echo Parameters entered: ${*} #! spin through the parameters and print each one separately for parm in ${*}; do echo ${parm} done fi $ Finally, here's a way to pump multiple files through sed: > ls *.java | xargs cat | sed -e 's/java/JAVA/g' public class BuckC { public static void main(String[] argv) throws Exception { System.out.println("JAVA.ext.dirs=" + System.getProperty("JAVA.ext.dirs")); System.out.println("JAVA.home=" + System.getProperty("JAVA.home")); System.out.println("sun.boot.class.path=" + System.getProperty("sun.boot.class.path")); System.out.println("JAVA.class.path=" + System.getProperty("JAVA.class.path")); System.out.println("JAVA.vm.version=" + System.getProperty("JAVA.vm.version"));

System.out.println("JAVA.version=" + System.getProperty("JAVA.version")); } } $ In order, ls *.java creates a list of all Java source files in the current directory. That gets piped into xargs which uses each file name as an argument to the cat command. cat copies the contents of the file to standard output. That gets piped into sed which processes an expression (e) that substitutes upper case JAVA for lower case java. sed writes that to standard output by default. One last treat. Scan all QRPG* files in your library for a particular string: find /qsys.lib/buck.lib -name QRPG*.FILE | xargs find ${1}.MBR | xargs grep -i 'genuuid'

S-ar putea să vă placă și