tcsh - test for character in string for if then


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers tcsh - test for character in string for if then
# 1  
Old 08-09-2011
tcsh - test for character in string for if then

I apologize if this treads already covered ground (which I sure it must have), however I wasn't able to determine what I needed from searches.

I'm trying to do detemine if a string represents a file name or not (i.e., is in the form "string.ext" or just "string"), by seeing if there's a period in the string.

I've created the string by parsing a list of paths. Some are paths to folders, some directly to files (this is a list of things to zip up into an archive). Like so:

/Volumes/tester/folder1
/Volumes/tester/folder1/test.txt

etc.

By awking via the "/" I can find the last part of the path. Now I just need to determine if it's a "string" or a "string.ext".

I already have the string set in a variable. I know I could do something like:

Code:
set MYVARIABLE	= `echo $string | awk -F. '{print $1}'`

which works perfectly if the string is in the "string.ext" form. However I only want to do that if I already know there's a period in the file name.

So is there a way to test the string first for a period and then do what I need to do based on that result?

Thanks for any help!

Last edited by deepstructure; 08-09-2011 at 04:00 AM.. Reason: added code tag
# 2  
Old 08-09-2011
Try:
Code:
set MYVARIABLE	= `echo $string | awk -F. 'NF > 1 {print $1}'`

I'm sure there is a better way to do your work (with test command) but I know about csh programming almost nothing,
# 3  
Old 08-09-2011
Hey yazu,

Thanks for that solution - that at least either gives me the result or an empty string. Now I need to do a search to figure out how to test the variable for a null value...

---------- Post updated at 04:10 PM ---------- Previous update was at 03:58 PM ----------

O.k, a combo of yazu's solution and a null string check does the job:

Code:
foreach ITEM ($DIRLIST)
	set SERVER			= `echo $ITEM | awk -F/ '{print $3}'`
	set SERVER_TARGET 	= `echo $ITEM | awk -F/ '{print "/"$2"/"$3"/"}'`
	set SOURCE			= `echo $ITEM | awk -F$SERVER_TARGET '{print $2}'`
	set SLSH_CNT_PRE	= `echo $ITEM | awk -F/ '{print NF-1}'`
	@ SLSH_CNT		= $SLSH_CNT_PRE + 1
	set LAST_PART		= `echo $ITEM | awk -v var1="$SLSH_CNT" -F/ '{print $var1}'`
	set TESTER  = `echo $LAST_PART | awk -F. 'NF > 1 {print $1}'`
	echo "----- $ITEM Variables -----"
	echo "slash count is   :$SLSH_CNT"
	echo "last part is     :$LAST_PART"
	if ( ${%TESTER} == 0 ) then
		echo "$LAST_PART does not have a period in it."
	else
		echo "$LAST_PART has a period in it."
	endif
	echo ""
end

which when run on the following list:

/Volumes/projects/_library/assets/textures/SeaLife/seaweed/seaweed01_base_v2.jpg
/Volumes/projects/_library/assets/digital-doubles/dd_sean/txt/DD_sean_splint_color.exr
/Volumes/projects/es/0697/3d/cam/_PUBLISH

produces the following result:

----- /Volumes/projects/_library/assets/textures/SeaLife/seaweed/seaweed01_base_v2.jpg Variables -----
slash count is :10
last part is :seaweed01_base_v2.jpg
seaweed01_base_v2.jpg has a period in it.

----- /Volumes/projects/_library/assets/digital-doubles/dd_sean/txt/DD_sean_splint_color.exr Variables -----
slash count is :10
last part is SmilieD_sean_splint_color.exr
DD_sean_splint_color.exr has a period in it.

----- /Volumes/projects/es/0697/3d/cam/_PUBLISH Variables -----
slash count is :9
last part is :_PUBLISH
_PUBLISH does not have a period in it.


Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcsh, using " in a string variable

Hallo, I try to write a tcsh-script which works with ImageMagick. The following command line works perfectly: convert a.tif -pointsize 80 -draw " gravity NorthWest fill black text 0,12 a " b.tif I use the following code in a script (it is a minimal example to show the problem): ... (3 Replies)
Discussion started by: DanielDD
3 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. Shell Programming and Scripting

Test command with special character not work

Hi all, Case 1 : A=88^M && echo "PASS" Result: PASS Case 2: A=88 && echo "PASS" Result: PASS I would like to know why Case 1 and Case 2 got the same result? What make ^M ignored ? Thanks in advance. (6 Replies)
Discussion started by: montor
6 Replies

4. Shell Programming and Scripting

Pass string to nummerical value in tcsh

I want to compare two values for Value A and ValueB, however, they are strings, so is there a way I could them into numberical value in tcsh #! /bin/tcsh set ValueA = `awk '{print $6}' out0File` set ValueB = `awk '{print $6}' out1File` set Delta = `expr $ValueA - $ValueB` ... (1 Reply)
Discussion started by: proteinpi
1 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

8. Shell Programming and Scripting

escape character in tcsh

Hello, I wanted to display command with echo like this in tcsh. output should be "//'test.test1.test2'" (" at both ends also required in output) Please help me. (1 Reply)
Discussion started by: balareddy
1 Replies

9. Shell Programming and Scripting

Matching a choice of character in test command

Hi ] && exit 0 Although it, the $answer, is 'y', the test operation returns true. && exit 0 This works but I want to do multiple choice matching. I don't want to do like: Please help (2 Replies)
Discussion started by: lalelle
2 Replies

10. Shell Programming and Scripting

Test return character.

Hi, in my korn shell I have this code: typeset -uL1 rc read rc?"Insert Y=Yes (default) or N=No >>" If I press enter without value I wish to set rc=Y. This is my default. This test: if ] then .... Do not work. I hope in your help. Thanks in advance. Regards, Giovanni (3 Replies)
Discussion started by: gio123bg
3 Replies
Login or Register to Ask a Question