13 Replies - 417 Views - Last Post: 17 December 2013 - 10:46 PM Rate Topic: -----

#1 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Korn Shell Coding (KSH)

Posted 16 December 2013 - 02:52 PM

Having a big time learning curve difficulty with ksh script.

I am taking the class next semester, and wanted to try and get a jump on the class, but for some reason the ksh is just not making sense.

I want to be able to write a piece of script that when executed with one argument, outputting whatever is typed on the keyboard to a file named in that argument. Ending input with lime a command D command or something like that. Furthermore, if there are two arguments, then it would copy the input file (named by the second argument) into the file the first argument named.

Anything other than one or two arguments would then print out a usage message giving its correct use.

I am honestly totally lost at even putting down a first line.

Any help with an explanation would be appreciated!

TCP

Is This A Good Question/Topic? 0
  • +

Replies To: Korn Shell Coding (KSH)

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:00 PM

Can you give us an idea of what you've tried, or what you think might work? It's a lot easier to help you if we can get a sense of where you're at already.
Was This Post Helpful? 0
  • +
  • -

#3 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:08 PM

Well I was given this to start, I haven't really tried anything. The exact text from the email I got from my professor was as follows

Quote

Write a ksh script which is called typer.ksh . It has one required and one optional argument.
If typer.ksh is executed with one argument, it outputs whatever is typed on the keyboard to the file named by the first argument. Input will end when the user types a Control-D The script then exits with a status code of zero.
If typer.ksh is executed with two arguments then it copies the input file (named by the second argument) into the file named by the first argument. The script then exits with a status code of zero.
If typer.ksh is executed with zero arguments or more than two arguments then it prints out a usage message giving its correct use, e.g., typer.ksh output_file_name [input_file_name]
and the script exits with a status of 1.


I was given very little info as it is just a Winter Break get ahead practice assignment.

TCP
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:13 PM

Does it remind you of any of the standard commands?
Was This Post Helpful? 0
  • +
  • -

#5 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:14 PM

This would literally be the first time I have ever dealt in ksh coding.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:17 PM

I suggest you spend a little time with tutorials and get familiar with the material. Someone's already taken the trouble to write down everything I could possibly think of to tell you, and they've probably done a better job than I would.
I'm happy to help spot-check your code, but no need to reinvent the wheel...
Was This Post Helpful? 0
  • +
  • -

#7 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:20 PM

ummm, ok. I don't even know what program I would use to write this code down. I am on a mac.
Was This Post Helpful? 0
  • +
  • -

#8 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 03:40 PM

And as I said, I have been looking I came here because I am totally lost.

I dont understand the theory behind scripting. I don't exactly understand what arguments are, I don't really get any of it, which is why I asked.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 04:02 PM

Well, if you have particular questions, that's a lot easier. As I say, if you want an overview there are plenty to choose from but if you have a specific issue, asking here will probably get good results. (maybe from me, or from someone more knowledgeable than me)

Here's a few tips:
To start using the command line, open the Terminal program. That'll launch you into some shell, probably bash.
To find out what shell you're using by default, type

> echo $SHELL

('>' represents the command prompt, which looks different depending on how you've configured it. For example, on my machine it looks like this:

[jon: Mon Dec 16 17:47]
/Users/jon/code/GBE:552 $


echo is a command that sends some output to the standard output. In this case, it's sending the value of the variable $SHELL, which is already set up for you. (You can change it, for example if you want to be in ksh by default - this is a good research topic for you to work on)


You can edit shell scripts in any pure text editor, including the TextEdit program that ships with your mac. However, I suggest you try to learn some flavor of vi - for example, vim - which goes nicely for this purpose. Emacs is another good option. Both of these have a little bit of a learning curve if you're used to pointy-clicky editors, but the work is worth it.
vi and its variants are a little easier, I find, to get started with than emacs, mostly because emacs is a huge program and you can get lost in it real easily.

To find out if a particular program exists on your machine, the "which" command is useful.

On my machine:

[jon: Mon Dec 16 17:59]
/Users/jon/code/GBE:553 $ which vi
/usr/bin/vi


This tells me that vi exists on my machine, and it will execute the program located at /usr/bin/vi

On the other hand,

[jon: Mon Dec 16 17:59]
/Users/jon/code/GBE:553 $ which foo


returns no result, which tells me that there isn't a command called "foo" on my path.

Anyway, get busy with the google - you'll have plenty of questions once you've worked your way through some examples.
Was This Post Helpful? 0
  • +
  • -

#10 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 120
  • View blog
  • Posts: 1,681
  • Joined: 28-June 08

Re: Korn Shell Coding (KSH)

Posted 16 December 2013 - 04:04 PM

so, to put it simply, a script is just that. A SCRIPT of commands (and usually differs from a full programming language in that. While most modern scripting languages incorporate a lot of programming elements, they aren't compiled into a binary). So, whereas on linux I might type hostname and then uname -a, to make that into a script, you would put it into a file, just like that.

hostname
uname -a



and it could be executed like that. It just follows a script of what commands to run, from top to bottom.

as for arguments, you are basically giving it variables to use. So, if I have a script called MyScript, and I write it to expect one argument (often built in variables, such as $1 for the first argument), say your name and output "Hello $1" where $1 is the argument, then anything you enter AFTER the name of the script, will get passed as the argument. While you COULD ask for input, it is generally considered bad practice in most cases.

EDIT: Haha, beat me to it Jon :P

This post has been edited by Gorian: 16 December 2013 - 04:05 PM

Was This Post Helpful? 0
  • +
  • -

#11 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 17 December 2013 - 09:17 PM

Ok, I think I have a pretty good question here.

The assignment I was given asks about being executed with one or two or more arguments. What does that mean exactly? And how would I decide to execute an already read script with a specific number of arguments.
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Dic Head
  • member icon

Reputation: 5163
  • View blog
  • Posts: 26,830
  • Joined: 10-May 07

Re: Korn Shell Coding (KSH)

Posted 17 December 2013 - 09:19 PM

Arguments are passed to a command on the command line

#!/bin/ksh
echo $1
echo $2
echo $3



Run this like : sh script.ksh one two tree

This is REALLY intro stuff that is easily found with Google or a good Korn Shell book.
Was This Post Helpful? 0
  • +
  • -

#13 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 17 December 2013 - 09:24 PM

ok I have had an absolute wave of understanding

#!/bin/ksh

if (( ${#} = 1 )) ; then
	#write to file
fi


if (( ${#} = 2 )) ; then
	#copy to input file names by first argument
	#how do you exit with zero
	exit 0
	
fi


if (( ${#} = 0 || ${#} > 2 )) ; then
	#exit with error message
	exit 1
fi


Am I on the right path? and how do you edit posts?

Ah, thats how....
Was This Post Helpful? 0
  • +
  • -

#14 TheCrownedPixel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 16-December 13

Re: Korn Shell Coding (KSH)

Posted 17 December 2013 - 10:46 PM

EDIT

Ok, this is what I have got for the first requirement, however I cannot seem to get it to write 'word' to a file. It should be picking up what I write to the keyboard. Also, I am not understanding why my second declaration will not copy one of the files to the other.

I am doing a lot better I think!

#!/bin/ksh

if [ ${#} -eq 1 ] ; then
echo "Please write what you want to add to the file now: "
read $word > /Users/thosebaby/Desktop/test.txt
exit 0
fi

if [ ${#} -eq 2 ] ; then
echo "The file $2 has been copied to the file $1."
cp $2 $1
exit 0
fi

if [[ ${#} -lt 1 || ${#} -gt 2 ]] ; then
echo "ERROR: Enter one argument to write to the file enter in a second to copy one file to another. You cannot write more than one file."
exit 0
fi


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1