Finding file extension on C Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding file extension on C Shell script
# 1  
Old 11-06-2014
Finding file extension on C Shell script

I very new to C Shell. I am trying to do is read from Command line.
Find the if the file is zip, .txt, symbloic link,pipe, unknow (if file is not zip, txt, sy....)
here is what I what got so far. I am very stuck atm Please help me out :


If the file is symblooc link what file is link to

Code:
!/bin/csh

if ( "echo ${1} | cut -d'.' -f2" == "zip" )  then
   echo $1 is a zip file
else
   echo sorry, $1 is not a zip file
endif


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks


---------- Post updated at 11:15 AM ---------- Previous update was at 11:04 AM ----------

Can someone help me out here?
# 2  
Old 11-06-2014
Look for case statement, Why use csh?
# 3  
Old 11-06-2014
Quote:
Originally Posted by madbull41
I very new to C Shell. I am trying to do is read from Command line.
Find the if the file is zip, .txt, symbloic link,pipe, unknow (if file is not zip, txt, sy....)
here is what I what got so far. I am very stuck atm Please help me out :


If the file is symblooc link what file is link to

Code:
!/bin/csh

if ( "echo ${1} | cut -d'.' -f2" == "zip" )  then
   echo $1 is a zip file
else
   echo sorry, $1 is not a zip file
endif

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks


---------- Post updated at 11:15 AM ---------- Previous update was at 11:04 AM ----------

Can someone help me out here?
The file name is irrelevant - the data inside the file is the data inside the file.
# 4  
Old 11-06-2014
We use C Shell atm. Here is more clear information C shell script print information about filespace objects.
# 5  
Old 11-06-2014
Your code can be optimized like this:
Code:
#!/bin/csh
if ( "${1:e}" == "zip" ) then
  echo "$1 is a zip file"
else
  echo "sorry, $1 is not a zip file"
endif

A "case" statement in C-shell looks like this
Code:
switch ( "${1:e}" )
case zip:
  echo "$1 is a zip file"
  breaksw
case zap:
  echo "$1 is a zap file"
  breaksw
default:
  echo "sorry, $1 is neither a zip nor a zap file"
endsw

# 6  
Old 11-06-2014
Quote:
Originally Posted by MadeInGermany
Your code can be optimized like this:
Code:
#!/bin/csh
if ( "${1:e}" == "zip" ) then
  echo "$1 is a zip file"
else
  echo "sorry, $1 is not a zip file"
endif

A "case" statement in C-shell looks like this
Code:
switch ( "${1:e}" )
case zip:
  echo "$1 is a zip file"
  breaksw
case zap:
  echo "$1 is a zap file"
  breaksw
default:
  echo "sorry, $1 is neither a zip nor a zap file"
endsw

"${1:e}"
what is mean?

$1 is command line right?
Code:
switch ( "${1:e}" )
case txt:
  echo "$1 is text"
  breaksw
case zap:
  echo "$1 is text"
  breaksw
default:
  echo "sorry, $1 is neither a zip nor a zap file"
endsw

[/QUOTE]

I mean in simple folder there just not one file in there. There will be a lot of file like txt pipe and all thing i mention above!
# 7  
Old 11-06-2014
$1 is the first argument when one runs the script (on the command line or from another script), for example
Code:
./yourscript example.zip

${1:e} is the "filename extension" i.e. the part after the last dot (and file type in Windos).
The e is a modifier. See
Code:
man csh

for other modifiers.
C-shell wants to expand/match/substitute a $variable so always quote it "$variable". Or, more general, have them in quotes like the following two variables
Code:
echo "$var1 and ${var2:e}"

NB you need the braces if you use a modifier. Without a modifier you can still use braces: echo "${var1}"

Last edited by MadeInGermany; 11-06-2014 at 01:55 PM.. Reason: code tag missing
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with shell script - not finding files

Hello, I have a shell script like the one below but cannot get it working: #!/bin/bash #$ -cwd CHR=$2 # directories ROOT_DIR=./ RESULTS_DIR=${ROOT_DIR}results/ # executable SHAPEIT_EXEC=${ROOT_DIR}shapeit.v2.r727.linux.x64/shapeit.v2.r727.linux.x64 # parameters THREAT=5... (1 Reply)
Discussion started by: wmelroy
1 Replies

2. Shell Programming and Scripting

shell script to change the extension of a file

I have a directory that contains several files, out of which some files are have an extra extension for example file1.new.new.new file2.new.new.new file3.new.new.new file4.new.new.new i want to write a shell script that rename all such file with only single extension like file1.new... (7 Replies)
Discussion started by: mukulverma2408
7 Replies

3. Shell Programming and Scripting

shell script for finding average runtime of other script

so I've made a shell script that downloads 6 files in succession from a given url, then deletes them. Now I want to time the script, and the average time it uses by running it ~100 times. My problem is tho, how do I store the time it takes for each run through of the script? I know time writes to... (3 Replies)
Discussion started by: navlelo
3 Replies

4. Red Hat

Need help with a shell script for finding a certain pattern from log file

Hell Guys, Being a newbie, I need some help in finding a certain string from a log file of thousands of lines (around 30K lines) and have the output in a separate file. Below is the file output - 10.155.65.5 - - "POST... (15 Replies)
Discussion started by: rockf1bull
15 Replies

5. Shell Programming and Scripting

Extracting file name and finding extension of the file

Hi., How to extract the extension of a file which are present in the directory?. In my script I tried something like: echo abc_ss_222_54.txt | awk -F"" '{print $5}' But I din't got .txt echoed in console. Pl. suggest a solution for this. And also, for finding a specific... (1 Reply)
Discussion started by: IND123
1 Replies

6. UNIX for Dummies Questions & Answers

Shell script to rename or change file extension case.

I searched the forum, but there was different type of rename. Hello. I have files in folder. Like: xxxxxxxx1.html or xxxxxxxx2.txt or xxxxxxxx3.tar.gz and how to rename or change file extension case to xxxxxxxx1.htm or xxxxxxx2.TXT or (5 Replies)
Discussion started by: Sheldon
5 Replies

7. Shell Programming and Scripting

Csh script get file extension

Hi All I have a csh shell script which should check if a given file is a zip file as below: **************************************** #!/bin/csh -f if ]; then echo is a zip file else echo sorry not a zip file endif exit **************************************** ... (1 Reply)
Discussion started by: raj144
1 Replies

8. Shell Programming and Scripting

CSH - finding files with the same file extension

Hey, I'm having a problem finding out if a directory holds any files with a certain file extension, for example .txt. I have tried: if (! -e "*.txt") and I've tried: set FILES=`ls *` echo "Found $FILES" foreach FILE ($FILES) if($FILE == "*.txt") And neither of... (2 Replies)
Discussion started by: amnesiaiom
2 Replies

9. Shell Programming and Scripting

shell script string extension

hey, im looking for a way of extending a string in shell script. for example i have two strings "." and "abcd", i need to extend the first string so that it is the same length as the second. so "." and "abcd" becomes "...." and "abcd", could someone shed light on how to do this ? thanks (4 Replies)
Discussion started by: vbm
4 Replies

10. UNIX for Dummies Questions & Answers

shell script: forbid extension

Rather new to unix, so please don't beat me! I'm trying to get a list of files into a variable that I can use throughout the rest of the script. The challenge is that I need to exclude a certain extension from the list, and I'm having trouble with it. For example: item_a item_a.exe... (3 Replies)
Discussion started by: Loriel
3 Replies
Login or Register to Ask a Question