Sponsored Content
Top Forums UNIX for Dummies Questions & Answers ksh script - not getting output from ls Post 302735467 by bakunin on Sunday 25th of November 2012 07:44:16 AM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy