1. The problem statement, all variables and given/known data:
create a shell script, chExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names of files to be renamed. For each file in the command line, this script should rename the file, as above, if the file exists or print the error message described in the previous step if the file does not exist.
For example,
ls > crocodile.foo
echo bark > dingo.bar
./chExt.sh dat crocodile.foo bogusName.foo dingo.bar
should result in crocodile.foo being renamed crocodile.dat, an error message "bogusName.foo: No such file", and dingo.bar being renamed dingo.dat.
2. Relevant commands, code, scripts, algorithms:
Code:
#!/bin/csh
set ext = $1
shift
foreach file ( $* )
# with double quotes show all to one name
# with single quotes foreach: No match.
# without quotes separate king cobra.dat into 2 files
echo "original input" "$file"
set oldName = "$file"
if ( -e "$oldName") then
set newName= (`echo "$oldName" | sed "s/\(.*\)\..*/\1/"`)
set newName = "$newName"."$ext"
if ("$file" != "$newName") then
mv "$file" "$newName"
endif
else
echo "$file"": No such file"
endif
3. The attempts at a solution (include all code and scripts):
Line 6 : foreach file ( $* )
I need it to be able to read in 'cpp' 'file.dat' 'king cobra.dat'
as 3 variables, but i can only get it to do 'king' and 'cobra.dat'
i tried putting double quotes around "$*" in my foreach but that causes the variable to be 'file.dat king cobra.dat' as one long variable name
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Old Dominion University, Norfolk, Virginia, USA, Prof. Zeil, CS252<http://www.cs.odu.edu/~cs252/>
please help ASAP, it's due by tonight and i've been stuck on this for days now.
Last edited by Corona688; 12-07-2012 at 02:42 PM..
C shell is infamous for quoting issues like this. In Bourne you would just use "$@" to split on parameters instead of spaces, but csh does not have this.
Instead of using the foreach loop, I'd use a while-loop, so you can just use "$1" and not worry about splitting anywhere.
C shell is infamous for quoting issues like this. In Bourne you would just use "$@" to split on parameters instead of spaces, but csh does not have this.
Instead of using the foreach loop, I'd use a while-loop, so you can just use "$1" and not worry about splitting anywhere.
Code:
while ( $# > 0 )
echo "got $1"
shift
end
I replaced the foreach loop with
while ( $# > 0 )
and the csh -x debugger stops at that line with:
Illegal variable name.
Was it more to it than just replace foreach with the while?
I even ran just:
Code:
#!/bin/csh
while ( $# > 0 )
echo "got $1"
shift
end
Also, can you (really, really) ask Professor Zeil why he is teaching you this crap?
I mean... C-Shell is so 1980's! Oh, and it's useless.
I have no idea why he chose c-shell. We could also use sh but csh looked easier.
This ends with Illegal variable name.
Code:
#!/bin/csh
while ( $# > 0 )
echo "got $1"
shift
end
as well as my full code with the while loop
Code:
#!/bin/csh
set ext = $1
shift
while ( $# > 0 )
# comments refer to issues with the forreach loop
# with double quotes show all to one name
# with single quotes foreach: No match.
# without quotes separate king cobra.dat into 2 files
echo "original input" "$1"
set oldName = "$1"
if ( -e "$oldName") then
set newName= (`echo "$oldName" | sed "s/\(.*\)\..*/\1/"`)
set newName = "$1"."$ext"
if ("$1" != "$newName") then
mv "$1" "$newName"
endif
else
echo "$1"": No such file"
endif
shift
end
If csh is not a requirement - and it's certainly not easier than sh, then I'd use something else. This is in Korn shell, and works in Bash too.
Code:
$ cat mySctipt
#!/bin/ksh
EXT=$1 # new extension in $1
shift
while [ $# -gt 0 ]; do
if [ ! -f "$1" ]; then # if the file doesn't exist
echo "File not found: $1" # Report error
shift
continue
fi
echo ${1%.*}.$EXT # Remove everything after . (the extension) and add the desired extension.
shift
done
$ ./myScript dat crocodile.foo bogusName.foo dingo.bar
crocodile.dat
File not found: bogusName.foo
dingo.dat
If csh is not a requirement - and it's certainly not easier than sh, then I'd use something else. This is in Korn shell, and works in Bash too.
Code:
$ cat mySctipt
#!/bin/ksh
EXT=$1 # new extension in $1
shift
while [ $# -gt 0 ]; do
if [ ! -f "$1" ]; then # if the file doesn't exist
echo "File not found: $1" # Report error
shift
continue
fi
echo ${1%.*}.$EXT # Remove everything after . (the extension) and add the desired extension.
shift
done
$ ./myScript dat crocodile.foo bogusName.foo dingo.bar
crocodile.dat
File not found: bogusName.foo
dingo.dat
I tried that and it came back with:
Code:
./myScript: 1: $: not found
crocodile.dat
File not found: bogusName.foo
dingo.dat
---------- Post updated at 03:33 PM ---------- Previous update was at 03:04 PM ----------
nevermind, thanks to you both for all the help.... I am finally finished with my CS252 class and will use ksh whenever i have the option over csh in the future.
Ubuntum, Bash version: 4.3.46
Hi,
how can I create a loop where the command line arguments change (increase) and every time the number of arguments is different ?
### I have many gene names... that mean gene1=$2, gene2=$3, ...... geneN=$N+1
### some time the number of gene is 25, other... (7 Replies)
Hi all,
I am trying to write a code for addition of n numbers which will be passed by the user as command line arguments.
I wrote the following code.
add=0
for (( i = 1 ; i <= $# ; i++ ))
do
add=`expr $i + $add`
done
#echo "sum is : $add"
input :
$./add.sh 12 32 14... (7 Replies)
HI ,
I am new to csh. I need to pass some command line arguments like
./abc.sh -os Linux -path abc -tl aa -PILX 1
I have defined the loop as shown below. But its taking "-os" switches as arguments. Its treating them as arguments.
How to resolve it?
while ( $#argv != 0 )
switch ($argv)
... (7 Replies)
hi,,,,
I want to create a command prompt, for example "prompt>", so my prompt need to handle commands, for example "prompt>cmd", so i want to know how to get arguments for my own commands cmd, i.e. default argc should contain arguments count and argv should point to the argument vector i.e, for... (2 Replies)
hi, can someone how to accept command line arguments as a variable using in script?
like: ./scriptname arguments
by accept arguments, I can use it in my script?
thx! (1 Reply)
I am working on a script wherein i need the user to enter the Build ID
for eg:the command line will show
enter the build ID
Now on entering the build ID it should be assigned to @ARGV.
How can this be done.? (1 Reply)
Hi,
When i am running the following script 1.sh (without giving the command line arguments) then i am getting the following error.
if
then
echo "UID and PWD are correct"
elif
then
echo "Either UID or PWD is wrong. Please check your UID and PWD"
else
echo "UID and PWD can't be blank"... (9 Replies)
--------------------------------------------------------------------------------
I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence... (1 Reply)
Hi all,
How many arguments can we pass while testing a prgm at command line..
I encountered an issue while passing 10 arguments.
For $10 its taking argument passed for $1 followed by 'zero'.
can we pass more than 9 arguments /Is there any other way.
Thanks,
rrs (6 Replies)
Hi
How to pass multi line text as a command line argument to a program.
(i.e)
./a.out hi this is sample 0 file1
where
hi this is sample should be stored in argv
0 in argv and so on... (3 Replies)