![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SunOS 5.8 performance issue | zing_foru | UNIX for Advanced & Expert Users | 1 | 09-08-2007 02:31 PM |
| Online UNIX emulator for Korn Shell scripting | eswasas | UNIX for Dummies Questions & Answers | 1 | 02-01-2007 04:46 PM |
| I can't decalar an array in my korn shell script | zilla30066 | Shell Programming and Scripting | 4 | 01-25-2007 11:31 AM |
| korn shell array? | cstovall | Shell Programming and Scripting | 5 | 01-28-2005 05:40 PM |
| Oracle To Korn Shell Array | kdst | UNIX for Advanced & Expert Users | 1 | 09-24-2002 09:21 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Unix Korn Shell Array Issue (SunOS)
Hello,
I'm currently messing around with arrays for the first time in scripting (Unix Korn Shell). All I'm trying to do right now before I make things complicated is read through and print out to screen whether the read file is or is not a directory. Here is my directory: ls -l total 16 -rwxrwxrwx 1 xrjbn3y staff 354 Nov 20 18:29 echodir -rw-r--r-- 1 xrjbn3y staff 136 Apr 5 2006 local.cshrc -rw-r--r-- 1 xrjbn3y staff 136 Apr 5 2006 local.cshrc.JASS.20050613104129 -rw-r--r-- 1 xrjbn3y staff 167 Apr 5 2006 local.login -rw-r--r-- 1 xrjbn3y staff 157 Apr 5 2006 local.login.JASS.20050613104129 -rw-r--r-- 1 xrjbn3y staff 184 Apr 5 2006 local.profile -rw-r--r-- 1 xrjbn3y staff 174 Apr 5 2006 local.profile.JASS.20050613104129 drwxr-x--- 2 xrjbn3y root 512 Nov 8 18:40 scripts Here is my script: #!/bin/ksh ########## i=0 max=`ls -l . | wc -l` set -A keiths `ls -l . | grep -v total | awk '{print $9}'` set -A filelist `ls -l . | grep -v total | awk '{print $1}' | cut -c1` while [ $i -le $max ] do print ${keiths[$i]} if [ ${filelist[$i]} = "d" ] then print ${filelist[$i]} else print 'Not A Directory' fi (( i=i+1 )) done Here are my results: ./echodir echodir Not A Directory local.cshrc Not A Directory local.cshrc.JASS.20050613104129 Not A Directory local.login Not A Directory local.login.JASS.20050613104129 Not A Directory local.profile Not A Directory local.profile.JASS.20050613104129 d scripts ./echodir[11]: test: argument expected Not A Directory ./echodir[11]: test: argument expected Not A Directory ./echodir[11]: test: argument expected Not A Directory I bolded within my script what the errors at the bottom of the result are not liking. With this being my first time using arrays, I'm not sure what needs to be corrected within the test to make sure the conditions are ok. If everything within the syntax is ok, is the script executing 3 more times for some reason? Any insight you guys can provide would be greatly appreciated. Thanks, Ryan |
|
||||
|
Half of this figured out, made the following corrections toying around with it:
Where I have the bolded piece of the script, replace that with: if [ $"{filelist[$i]}" = "d" ] As you can see, the " " are now added in the correct spot. The results now show the following: ./echodir echodir Not A Directory local.cshrc Not A Directory local.cshrc.JASS.20050613104129 Not A Directory local.login Not A Directory local.login.JASS.20050613104129 Not A Directory local.profile Not A Directory local.profile.JASS.20050613104129 Not A Directory scripts Not A Directory Not A Directory Not A Directory The errors are gone, but now I'm getting an extra 3 runs of the loop, working on debugging this but wanted to keep you all up to date on how to fix that sort of issue... Edit: The commands I run within the script to set up the arrays are the following, each file should have it's corresponding identifier as to whether its a file or directory etc. With the results above, it looks like "d" isn't detected at all... ls -l . | grep -v total | awk '{print $9}' echodir local.cshrc local.cshrc.JASS.20050613104129 local.login local.login.JASS.20050613104129 local.profile local.profile.JASS.20050613104129 scripts ls -l . | grep -v total | awk '{print $1}' | cut -c1 - - - - - - - d Last edited by Janus; 11-20-2006 at 08:40 PM.. |
|
||||
|
Hi - Don't have access to a sun box at this time but try this script...
#!/bin/ksh lslist="`ls -m | tr -d \",\"`" set -A lsarry $lslist start=1 while [ $start -lt ${#lsarry[*]} ] do if [ -d ${lsarry[$start]} ] then echo "-${lsarry[$start]}- is a directory" else echo "-${lsarry[$start]}- is not directory" fi start=`expr $start + 1` done Just check and make sure you have the "-m" option for ls and if you use a different dilimiter char in the list then change the tr statment. |
|
||||
|
Andrek, I put in your code and it executed to work great! I went one step further and added the following piece to list out any existing directories and it's corresponding files:
if [ -d ${lsarry[$start]} ] then echo "-${lsarry[$start]}- is a directory" cd ${lsarry[$start]} ls -l else I do have one more question however. As someone always learning in the Unix Scripting field, I was wondering if you could explain what a few of the expressions are doing? lslist="`ls -m | tr -d \",\"`" set -A lsarry $lslist start=1 while [ $start -lt ${#lsarry[*]} ] This is where I get confused. I understand that you are asking if the index is less than the value in the array, but what does the # before the lsarry mean? I take it that the[*] means the last array slot to contain a value? do if [ -d ${lsarry[$start]} ] I'm unfamiliar with -d before the array. Does this mean "IF 'D' = ArrayValue?" How is the code being interpreted? then echo "-${lsarry[$start]}- is a directory" cd ${lsarry[$start]} ls -l else echo "-${lsarry[$start]}- is not a directory" fi start=`expr $start + 1` done Everything after the first two questions in italics I understand, if you could explain those two just a little bit more, it would be greatly appreciated. Thanks! |
|
||||
|
Ok I'll do my best...
while [ $start -lt ${#lsarry[*]} ] Simple while loop. It keeps looping until the var $start is lessthan the total number of elements in the arry. eg ${#lsarry[*]} would be equall to 4 if the array is made up as follows "A B C D" The # indicates to give the "count" of the elements in the arry and the "*" simply indicats ALL the elements.... (I guess its like a wild card...ie ls *-> which list every thing) the "-d" is a test for a directory. Does it mean "IF 'D' = ArrayValue?" NO. Do a man on "test" and you will see all the options. UNIX will "test" the input and return true if the path/to/target is a directory or false if not. How is it being inerpreted? if [ -d ${lsarry[$start]} ] How is the code being interpreted? - as you know $start is increased by 1 each loop so the "next" element in the arry is placed into the "test" statment and evaluated....ie ${lsarry[1], ${lsarry[2], etc.... you can run the script with full debug on to see whats happening by either... 1) add set -x to the second line ....or 2) # sh -x {scriptname} Hope this helps. PS do a man on sh-bourne or man on ksh and near the bottom it talks about arrays and what "#" etc means.... Have fun :-) |
|
||||
|
Awesome, that was perfect, just what I needed. It's much easier to see where everything is going now. Having not worked with array's yet, that explanation cleared up the questions I had about the code. A friend of mine saw what I was working on and asked, "Why not use 'ls -lR'? When I used that command I noticed it worked in a similar fashion. I'm always learning, that's the beauty of Unix...Thanks again!
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|