Shell Scripting with Arguments

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell Scripting with Arguments
# 1  
Old 08-14-2012
Shell Scripting with Arguments

1. The problem statement, all variables and given/known data:

Problem 1:

I need to create a shell script the takes three arguments and echo's out "There once was a ____that____who like to_____"
The arguments go where the blanks are.



Problemt 2:

-Do an LS and store the listings in a file

-Open the file in emacs.

-create a macro that converts that single line to the command "mv file.txt file.dat"

-run the macro over the entire file.

-chmod +x the listing file

-run the file as a script.





2. Relevant commands, code, scripts, algorithms:

For problem one my instructor provides me with this example:

echo -n "some argument"

echo $1

how I've been reading it is like a C++ code

cout << "Some argument"

cin >> argurment

3. The attempts at a solution (include all code and scripts):

For problem 1 I've tried it many different ways, this is my latest attempt:

echo -n "There once was a "
echo $1
echo -n "that "
echo $2
echo -n "who liked to "
echo $3
echo -n "There once was a " $1 " that " $2 " who liked to " $3

I believe my teacher wants the echo to be just on one line. I dont want the code given to me, I'm really just hoping someone can show me what I'm doing wrong so I can correctly do it. There is a part b to this question and if I understand what I'm doing wrong in part a im positive I can do part b.

For problem 2 I've run an ls and moved all .txt files to a new file called change. From there I created a macro that searched for all .txt and replaced them with .dat. I'm having a hard time understanding how to mv this data to replace the old .txt files in the directory I copied them from.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Portland Community College, Hillsboro, OR, United States, Mike Noel, CS 140u Intro to Unix

Link -PCC Class Schedule

Last edited by dw15; 08-14-2012 at 08:15 PM..
# 2  
Old 08-14-2012
For problem 1: What happened wrongly with this: echo -n "There once was a " $1 " that " $2 " who liked to " $3 ?

Do you know what the -n in echo -n does? Do you need it on that line?

Double quotes are "weak quotes". They allow variable expansions inside of them. Try using only one set of them.
# 3  
Old 08-15-2012
You described yourself as "done some C++" in your original thread and i suppose it is the difference between shell scripting and high level languages, which makes this hard to understand for you:

A single line in a HLL is read by a parser, then compiled to object code and finally this object code is executed. All the interpretation of the code is done at compile time and all the execution is done at run time. This is fundamentally different with shell scripting.

The big difference is that the interpretation part and the execution part takes place at the same time and in fact is a tightly interwoven process. The following, for instance, has no counterpart in any high-level language:

Code:
cmd="ls -l"
$cmd

In line one a variable is created and filled with a certain content. In the second line this variable is executed as command! Well, in fact not the variable is executed, but the command is: when the shell has to execute this line the first step is to "expand" all the variables, which means to literally replace them with their values. Only then the resulting line is executed.

This makes shell code very versatile (it was created with the purpose of "being able to quickly whip together something" in mind).

This evaluation process does not stop at variables. You sure have done something like this already:

Code:
ls -l *txt

The asterisk is also interpreted by the shell. It is replaced ("expanded") to the list of files matching the pattern (in this case "anything ending in 'txt'").

Consider the following line:

Code:
cmd="ls -l *"
$cmd

This is what the shell will do:

Code:
cmd="ls -l *"
$cmd                           # first, replace the variable with its content
ls -l *                        # now replace the wildcard with the list of files
ls -l a.file b.file c.file     # finally execute this

(i supposed to have the three files "a.file", "b.file" and "c.file" in this directory)

The only way to prevent the shell from doing this expansion is to quote: use double quotes to prevent it from expanding wildcards (which is why it hasn't expanded the asterisk in line 1 already) and single quotes to prevent expansion of variables:

Code:
var="ls *"

echo $var          # results in "ls a.file b.file c.file"
echo "$var"        # results in "ls *"
echo '$var'        # results in a literal "$var"

I hope this helps.

bakunin
# 4  
Old 08-15-2012
Oh my. Your final is today. I imagine you've not checked back here in time, but I just realized your idea that echo $1 to be equivalant to cin >> argument. No, echo will output. $1 is assigned like ARGV[]. It is the first of the arguments given on the command line when executing, or to a function. Such that ./script teacher 'disliked those' 'code python' would complete the sentence as "There once was a teacher that disliked those who like to code python"
# 5  
Old 08-15-2012
Yeah, I just now got a chance to look at everyones help, which, by the way, thank you! This is finals week for me and I've been stressing on this H.W. problem for about a week now. After reading through the post I have a much better understanding as to what I'm doing wrong here. I got about two hours from me posting this, until I have to go to school for another final, and then whatever time I get between finishing that final and going on to my CS final is all I can spend working on this. Seriously though thank you all for your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Beginner at bash scripting - need help with passing arguments

I at the moment, making a simple bash script, capable of setting up an workspace for me, so i don't have to do it manually.. Problem is though i can't seem to provide the bash script any argument, without running into my error checks, checking for input... Here is the code: #!/bin/bash... (7 Replies)
Discussion started by: kidi
7 Replies

2. Shell Programming and Scripting

Scripting with arguments for ping command

This is the script I already have but I have problems with two arguments the first argument -t , I want to count 200 by the last digit of the IP address for example when I run the script ./ping.sh -t 17, the output would be192.168.0.217 is upThe second arguments --up won't work. Could anybody... (1 Reply)
Discussion started by: Roggy
1 Replies

3. Shell Programming and Scripting

Shell scripting with passing arguments

Hi All, I am using the script for creating local queue and passing the arguments while running the script as below n=0 while do e=`expr $n + 3` echo 'DEFINE QL('$e') MAXDEPTH('$6') MAXMSGL('$7') DEFPSIST('$8') '$9'' | /apps/mqm_opt/bin/runmqsc $2 n=`expr $n + 1` done Running the... (5 Replies)
Discussion started by: Anusha M
5 Replies

4. Shell Programming and Scripting

sub arguments to shell script

Hi, I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit. What I am trying to do is run the shell script with the argument passed in however it does not seem to work. This is what I did ./test.sh 1 Instead it printed the line... (6 Replies)
Discussion started by: aqua9
6 Replies

5. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 Replies

6. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

7. Shell Programming and Scripting

Bash Shell - # of arguments

Hi!, How can I check the number of arguments passed from the shell in a bash shell ? (1 Reply)
Discussion started by: DNAx86
1 Replies

8. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

9. Shell Programming and Scripting

Shell script with arguments

Hi All, I need some help/ideas in coming up with a shell script. Basically, the script should install 1 or 2 or 3 packages based on the input arguments. For example, if I type in pkgscript.sh a1 a2 a3, it should install all the 3 scripts and pkgscript.sh a1 should install only a1. If a... (3 Replies)
Discussion started by: sankar6254
3 Replies

10. UNIX for Advanced & Expert Users

Arguments to a shell program

Hi List, Is it possible to pass one argument to a shell program eg) there is a shell program abc which takes one arguments abc one Due to some reasons I pass abc one two Now one,two must be considered as "one" argument to the shell programs. Any suggestions,hints are welcome. ... (3 Replies)
Discussion started by: csvenkata
3 Replies
Login or Register to Ask a Question