ksh script - not getting output from ls


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers ksh script - not getting output from ls
# 1  
Old 11-23-2012
ksh script - not getting output from ls

Hi!
I'm a complete noob, and I'm trying to write a little script that takes a directory pathname as input from the CL, checks whether it exists, and if not creates it, then shows it worked using ls.
Everything works beautifully except in the first instance, wherein the directory is created, ls does not print its output. Running it in debug mode, it is clear that ls is executing, but I can't figure out why it doesn't show in stdout.
Code:
$ ksh -x ./newdir.ksh
+ print -n 'Name a directory that ought to exist: '
Name a directory that ought to exist: + read dir
Fun/Super/Duper/Pooper
+ [[ ! -d /Users/rick/Fun/Super/Duper/Pooper ]]
+ print 'Directory does not yet exist. I will make it so...'
Directory does not yet exist. I will make it so...
+ mkdir /Users/rick/Fun/Super/Duper/Pooper
+ ls -R /Users/rick/Fun/Super/Duper/Pooper

I've tried it a few different ways. I thought maybe ls was executing before mkdir finished, but && didn't help.
Code:
mkdir ~/$dir && ls -R ~/$dir

No sense going into all of them since none of them worked.
I've searched the web, and while there's a lotta info on redirecting to and from files, I couldn't find anything on making sure output directed to the screen. I guess because that would be the expected behavior. But I'm probably looking for the wrong thing. I'm such a complete noob, I don't know what to look for.
Anyway, it works fine in the second instance, where the directory already exists.
Code:
$ ./newdir.ksh
Name a directory that ought to exist: Fun
That directory does exist, as well it should.
Super

/Users/rick/Fun/Super:
Duper

/Users/rick/Fun/Super/Duper:
Pooper

/Users/rick/Fun/Super/Duper/Pooper:

Here is the script:

Code:
#!/bin/ksh

## If a directory does not exist, create it. Then let us see that it worked.

	print -n "Name a directory that ought to exist: "
read dir

if [[ ! -d ~/$dir ]]
then
	print "Directory does not yet exist. I will make it so..."
	mkdir ~/$dir
	ls -R ~/$dir

else
	print "That directory does exist, as well it should."
	ls -R ~/$dir
fi

No doubt I've overlooked something very fundamental and simple, but for the life of me, cannot figure it out.
# 2  
Old 11-23-2012
I copied your script and it ran fine. What OS?

Code:
ksh -x newdir.ksh 
+ print -n 'Name a directory that ought to exist: '
Name a directory that ought to exist: + read dir
testdir
+ [[ ! -d /home/testuser/testdir ]]
+ print 'That directory does exist, as well it should.'
That directory does exist, as well it should.
+ ls -R /home/testuser/testdir
/home/testuser/testdir:

# 3  
Old 11-23-2012
-R option will recursively list sub directories, ls is not listing anything since there are no sub directories to list inside the newly created directory.
# 4  
Old 11-24-2012
-R flag makes no difference

Quote:
Originally Posted by bipinajith
-R option will recursively list sub directories, ls is not listing anything since there are no sub directories to list inside the newly created directory.
Yes, but the newly created directory is itself a sub-directory, so ls -R should show that. And we see in debug mode that it does execute, and outputs the pathname, but the output never makes it to the screen. But I tried it without the -R flag, and it still doesn't work:
Code:
$ ./newdir.ksh
Name a directory that ought to exist: Fun/Super/Duper/Pooper
Directory does not yet exist. I will make it so...
$

Now, I can use the -v flag in mkdir, but the mkdir man page suggests:
Quote:
The -v option is non-standard and its use in scripts is not recommended.
But, ok, it's not like my little exercise is gonna end up on another system. Nevertheless, I would like to know what I'm doing wrong - that is, why ls works in the second instance, (if the dir already exists), but not the first, (if it has to create the dir).

---------- Post updated at 12:59 PM ---------- Previous update was at 12:47 PM ----------

Quote:
Originally Posted by in2nix4life
I copied your script and it ran fine. What OS?

Code:
ksh -x newdir.ksh 
+ print -n 'Name a directory that ought to exist: '
Name a directory that ought to exist: + read dir
testdir
+ [[ ! -d /home/testuser/testdir ]]
+ print 'That directory does exist, as well it should.'
That directory does exist, as well it should.
+ ls -R /home/testuser/testdir
/home/testuser/testdir:

Yes, it works when the directory already exists, but not when it has to create the directory. Try using it to create a new directory and see if it works for you.

Mac OS 10.6.8
ksh 93

Last edited by sudon't; 11-24-2012 at 02:30 PM.. Reason: clarity
# 5  
Old 11-24-2012
Quote:
Originally Posted by sudon't
Yes, but the newly created directory is itself a sub-directory, so ls -R should show that. And we see in debug mode that it does execute, and outputs the pathname, but the output never makes it to the screen. But I tried it without the -R flag, and it still doesn't work
I think you are confused, when ls is running it is actually looking if there are any sub-directories inside the newly created directory not the directory itself:
Code:
# ./newdir.ksh
Name a directory that ought to exist: DIR1
Directory does not yet exist. I will make it so...

Now create a directory inside the newly created directory: DIR1 and re-run the script, you will see ls listing the sub directory: SUBDIR1
Code:
# cd ~/DIR1
# mkdir SUBDIR1

Code:
# ./newdir.ksh
Name a directory that ought to exist: DIR1
That directory does exist, as well it should.
SUBDIR1

/home/user/DIR1/SUBDIR1:

I hope you understood.
This User Gave Thanks to Yoda For This Post:
# 6  
Old 11-24-2012
Quote:
I think you are confused, when ls is running it is actually looking if there are any sub-directories inside the newly created directory not the directory itself:
You mean it doesn't look above the newly created directory, only at, and below? Ok, ok, I see now. Yes, it took a minute to see how I'd gotten it to "work" in cases where the directory existed - I chose higher level directories to save typing, so they always had sub-directories for ls to show. Duh!
Here's how I solved it to get what I wanted:
Code:
	print -n "Name a directory that ought to exist: "
read dir

if [[ ! -d ~/$dir ]]
then
	print "Directory does not yet exist. I will make it so..."
	mkdir ~/$dir
	find ~/$dir -type d -print
else
	print "That directory does exist, as well it should."
	ls ~/$dir
	find ~/$dir -type d -print
fi

Seems to work:
Code:
$ ./newdir.ksh
Name a directory that ought to exist: Fun/Super/Duper       
Directory does not yet exist. I will make it so...
/Users/rick/Fun/Super/Duper
$ ./newdir.ksh
Name a directory that ought to exist: Fun/Super/Duper
That directory does exist, as well it should.
/Users/rick/Fun/Super/Duper

I should've done that in the first place - because that's what find does. I still feel a little intimidated by find, but it always turns out to be incredibly useful. I really need to learn that command.
Thanks, everyone!
# 7  
Old 11-25-2012
Of course there is nothing wrong with using "find" - but there are a few points I'd like to highlight. (Just between us experts - there are more ways to skin a cat, isn't it? ;-))

Code:
if [[ ! -d ~/$dir ]]
then
	print "Directory does not yet exist. I will make it so..."
	mkdir ~/$dir
	find ~/$dir -type d -print
else
	print "That directory does exist, as well it should."
	ls ~/$dir
	find ~/$dir -type d -print
fi

The first thing is a general observation: when you write some "if..else..fi" and you end up with commands in both the "if"- and the "else"-branches - they could be placed outside the "if" too, no?

Code:
if [[ ! -d ~/$dir ]] ; then
	print "Directory does not yet exist. I will make it so..."
	mkdir ~/$dir
else
	print "That directory does exist, as well it should."
	ls ~/$dir
fi
find ~/$dir -type d -print


The next thing is a minor detail, which will get more and more importance once you move from pet projects to doing real work: quoting. The shell uses blank characters to separate fields in the input. You implicitly rely on this when you write:

Code:
command arg1 arg2

and expect the shell to call "command" with two arguments, "arg1" and "arg2". You don't expect the shell to call "command arg1" (which would be a perfectly fine command name) with one argument "arg2" or something such. Now suppose the directory name you use contains a blank. It is perfectly legal for a unix file to have blanks in the name. The shell would take what you wrote and replace "$dir" with its contents and then try to digest what comes out (suppose the name to be "dir name"):

Code:
find ~/$dir -type d -print
find /home/sudont/dir name -type d -print

First off, "find" would now search for the wrong directory ("dir" instead of "dir name") and then it would be presented an argument "name", which it cannot identify - a syntax error would occur. This is why quoting is recommended always when you deal with values you don't create inside your script, like filenames. Write it like this:

Code:
find ~/"$dir" -type d -print

Even if "$dir" contains a blank the shell will be told to treat it as a single word - this is exactly what double quotes are for. If you ever come across the term "word splitting" - this is what the shell does per default and what double quotes switch off for their inside part.


Next detail: "mkdir" intrinsics.
You write:

Code:
mkdir ~/$dir

and this might well work - as long as you only enter ONE directory level to create. You can enter "super/duper/pooper" and as long as "super/duper" already exists it will create "pooper" as a subdirectory to "duper". But if "super/duper" (or parts of this hierarchy) will not exist the command will fail. This is why "mkdir" has the "-p" switch and experienced script programmers like you should take no unnecessary risk: either you WANT mkdir to fail in this case (then you should create the proper error handling logic like you did with the non-existing directory) or you should routinely use the "-p" switch:

Code:
mkdir -p ~/"$dir"

This will create "~/super/duper/pooper" by first creating "~/super" if it doesn't exist, then creating "~/super/duper" if this doesn't exist and finally "~/super/duper/pooper".

Last thing: "find" is not necessary in this case.
You were already on the right track with "ls", but "ls" per default shows nothing in an empty directory - which is why it looked to you like it failed. But fortunately there is always something in a directory, even a newly created one: the "current directory" entry named "." and the "parent directory" entry named "..". You shure have typed "cd .." to move "higher up" - this is the entry for that. Alas both these entries begin with "." and per default files starting with "." are excluded from display by ls. This is as close to "hidden files" known from DOS/Windows as Unix files get.

Usually this used for configuration files like "~/.profile" or "~/.bashrc". You probably do not want to see them when you do an "ls -l" but only the real files you have, so this is a sensible default. But any default can be overridden and this one can be too - with the "-a" switch of "ls". Therefore, if you want to use "ls" as originally intended, use:

Code:
ls -a ~/"$dir"

instead of "find".

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing output parameter(Oracle) to variable in ksh Script

Hello, I have researched and tried many way to pass OUT parameter to be stored in variable in KSH Script.Still not success, please help. Here is my Store Procedure. create procedure testout3(v_in varchar2,v_out OUT integer) as begin v_out := 1; end; Here is my testvout.ksh #!/bin/ksh... (1 Reply)
Discussion started by: palita2601
1 Replies

2. Shell Programming and Scripting

Line break in sqlplus output through ksh script

Hi, I am new to shell script programming. I have written a ksh script to run the sql File placed in server directory and spool the output in destination directory. Below Command: $ORACLE_HOME/bin/sqlplus -s $ora_uid @$sqlfile_loc$testquery.sql > $opfiledirectory It is generating the output... (6 Replies)
Discussion started by: Sumit Arora
6 Replies

3. UNIX for Dummies Questions & Answers

Executing ksh script to remote server want output on same window

I'm having a brain freeze moment. I've created a ksh script in AIX that ssh's to a remote server, executes some commands, and then logs out. All of this is sent to a file. I then have the script cat the file so i can see the output. Even though the cat command is outside of the remote session part;... (5 Replies)
Discussion started by: seekryts15
5 Replies

4. Shell Programming and Scripting

Output in for loop (ksh)

Hi , I'm writing the for loop script in home directory and wanted to get the files from /etc/data directory. #!/bin/ksh file_nm="/etc/dat" for test_data in $file_nm/fln* do echo "$test_data" done the code is executing successfully , but in the output it is showing ... (6 Replies)
Discussion started by: smile689
6 Replies

5. Shell Programming and Scripting

Help with ksh script to display output with specific contents

This is Input - starts with Storage Group Name and ends with Shareable and the loop continues all I need is Storage group name and Alu numbers in the below output format requested. Storage Group Name: abcd Storage Group UID: 00:00:000:00:0:0:0 HBA/SP Pairs: HBA UID ... (6 Replies)
Discussion started by: maddysa
6 Replies

6. Shell Programming and Scripting

ksh script to process grep output

Hi, I would like to know how can i pipe the following output of grep into a predefined output format This is the output of the grep command grep record *.txt | sort -r 2010-04-28-11-12-21.txt:C The user has created a record 2010-04-29-10-18-41.txt:U The user has updated a record... (8 Replies)
Discussion started by: alienated
8 Replies

7. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

8. Shell Programming and Scripting

ksh script that echo " please insert your name " and store the output to a login.log file.

Hello All Nice to meet you all here in this forum, it's my 1rst time here i'm asking about a little issue that i face i added a ksh script that echo " please insert your name " and store the output to a login.log file. the script is working fine with normal telnet but Xstart is not working... (8 Replies)
Discussion started by: islam.said
8 Replies

9. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies

10. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question