ksh script - not getting output from ls


Trivia Category: Science: Computers
Difficulty: Easy
The Windows 7 operating system has six main editions.
True or False?

 
Thread Tools Search this Thread
# 8  
Quote:
Originally Posted by bakunin
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?
No, I didn't know that. Do you mean, "in the then and else branches"? Although I've read that fi is simply if backwards, I've always read it as finish. You know, like, it's the end. I'm still not entirely clear on how these commands, or arguments, or whatever they're called, work.
But you're saying that I could could just put a single instance of find, or whatever command, after fi, and it'll execute no matter what happens above it? Yeah, ok, I can see that now.

I should also mention that I simply forgot to delete ls ~/$dir, which I intended to do. I like the output of find better, because prints only the path on one line, as opposed to what I was getting, or hoping to get, from ls.

Quote:
The next thing is a minor detail...
No, you're right, especially if we try to make something that might actually be useful. I'm thinking it's possible in many cases that a variable could be handed some white spaces. Do you think it's a good idea to always quote variables, as a matter of course?

Quote:
Next detail: "mkdir" intrinsics.
Right! Again, something I hadn't considered. I tried it, and as you suggest, it did not work. Adding the -p flag fixes it.

Quote:
Last thing: "find" is not necessary in this case.
While I did not try this - at least, not yet - I wonder if cd .. would always work, since it only takes you up one level? For instance, if you had empty directory /Fun, then specified, say, a new directory four levels down: /Fun/X1/X2/X3 Wouldn't cd .. only position you, (and ls), at /X2?
# 9  
Quote:
Originally Posted by sudon't
Do you think it's a good idea to always quote variables, as a matter of course?
Yes, absolutely. There are a very few occasions where not quoting has some desired effect and it not done. In the overwhelming majority of cases quoting has no bad effect at all and probably/perhaps a good effect. Quoting routinely is good by far more often than not.


Quote:
Right! Again, something I hadn't considered. I tried it, and as you suggest, it did not work. Adding the -p flag fixes it.
This is called "defensive scripting" and is generally a good state of mind: once you know what you want and basically how you want it to be done you begin to wonder how the procedure you intend to use could fail for various reasons. Then you start to take care of these reasons one by one.

Here is a real-life example from my practice: i once wrote a script-function which created space for temporary files. Basically this is a simple task: create a directory. Now i asked myself: how could this fail? The following list could easily be prolonged:
  • the desired directory already exists. Solution: make sure you create a directory with a name guaranteed to be unique.
  • The script has no write-access to the place where the directory is to be created - issue an error message.
  • The directory cannot be created because of no inode being available - issue a different error message.
  • The directory can be created but the available space in the filesystem is not big enough to hold the temporary files - issue yet another error message.

What i finally wrote was about 50 lines of code, which still basically did a "mkdir" - the rest was error checking or error reporting. But first it is a good thing to actually know why a script failed, instead of it just crashing and second, some of my scripts had to run on a world-wide installation base, close to 20.000 machines. Guess what, all of the paranoid ideas i came up with about what could go wrong - did go wrong on at least one system! Still, some systems easily exceed even your worst nightmare and some reasons for the script failing didn't even occur to me and were nevertheless happening. Go figure.

Quote:
While I did not try this - at least, not yet - I wonder if cd .. would always work, since it only takes you up one level? For instance, if you had empty directory /Fun, then specified, say, a new directory four levels down: /Fun/X1/X2/X3 Wouldn't cd .. only position you, (and ls), at /X2?
Exactly this is the case. Your home directory is perhaps "/home/sudont", so position yourself there and issue "ls -l .." - and you will get a listing of all the files/directories in "/home". Do a "ls -l ../.." and you will get the same for "/". This only fails if you try to go beyond the root directory "/" - for obvious reasons. Btw., these rules are the same for every command which takes a path as an argument: for "ls", "cd", "find", ... Every time ".." means the directory one level up from where you are now and "." means the directory you are right now. "Where you are now" is either your current working directory, PWD (the place you "stand at" in the filesystem), or a place the path you just enter is leading to: write "cd /foo/bar" and you will be taken to "/foo/bar", but writing "cd /foo/bar/./." has the same effect - it is like adding zero to a number: not changing anything.

Or write "cd /foo/bar/.." - this will take you to "/foo" (and is a very complicated way of getting there), because from "/foo/bar" one level up is "/foo".

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 10  
Quote:
I hope this helps.

bakunin
As always, very informative. Thanks!
"Defensive scripting" - I like that.

If I may, one more question: When the shell sees fi, does it mean, "quit considering the original question, the if? In the case of my script:
Code:
[[ ! -d ~/$dir ]]

This is what I gather from what you told me about putting a single instance of ls or find outside/below fi. I had thought fi meant, "this is the end of the script, stop working". Or as the French say, "fin." The book doesn't really explain that, at least, it hasn't yet.
# 11  
Quote:
Originally Posted by sudon't
I had thought fi meant, "this is the end of the script, stop working". Or as the French say, "fin."
Not quite. "fi" is indeed "if" spelled backwards (you will later learn a construct called "case" which ends with "esac") and it ends only the fi-clause, nothing else. Cosider the following (pseudo)-code:

Code:
command1
if [ some expression ] ; then
     command2
else
     command3
fi
command4

Control flow would be: execute command1 first. Then, if the condition in "expression" is met, execute command2, if not, execute command3. Regardless of the condition being met or not execute command4 then. Consider "if" and "fi" like a braces between to put something.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

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

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

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

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

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

8. Shell Programming and Scripting

Can ls in ksh write to output?

I have a ksh script written by someone... now i am trying to modify it to my requirement.... Its executed like this Read.ksh load ${CUR_MAINT_DATE} if then filnam="Load_CLM" else filnam="${1}" fi if then CUR_MAINT_DATE="${2}" fi if then ls... (1 Reply)
Discussion started by: bhagya2340
1 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

Featured Tech Videos