How to check & reset variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check & reset variables
# 1  
Old 06-17-2009
How to check & reset variables

Hi,

I'm dealing with a small problem here that I can't seem to overcome by myself. Any help would be greatly appreciated Smilie

Basically, I have three variables, EXTSUB1, EXTSUB2 and EXTSUB3. These variables carry a file the user provides. What I need is to check each of the variables and if the specific variables carries a file with a specific extension, then reset that variable. I currently have the following but would like to reduce this code, maybe even do it in one go for all three variables...

Code:
# Check file extensions and if they match the
# defined ones, reset the specific variable

case "${EXTSUB1##*.}" in
            ass|ASS|idx|IDX|sub|SUB|sup|SUP|ssa|SSA)
             echo
             echo "-> Subtitle format '${EXTSUB1##*.}' not supported!"
             echo "-> Skipping import..."
             EXTSUB1=
             ;;
esac
case "${EXTSUB2##*.}" in
             ass|ASS|idx|IDX|sub|SUB|sup|SUP|ssa|SSA)
             echo
             echo "-> Subtitle format '${EXTSUB2##*.}' not supported!"
             echo "-> Skipping import..."
             EXTSUB2=
             ;;
esac
case "${EXTSUB3##*.}" in
               ass|ASS|idx|IDX|sub|SUB|sup|SUP|ssa|SSA)
               echo
               echo "-> Subtitle format '${EXTSUB3##*.}' not supported!"
               echo "-> Skipping import..."
               EXTSUB3=
               ;;
esac

Any better way of doing this with smaller code?

Thanks

-----------------------------------

OK, so I found away to use arrays for the job... Currently I have the following and it seems to work after some testing.

Code:
            for i in {1..3}; do
			if [ ! -z "${EXTSUB[$i]}" ]; then
				case "${EXTSUB[$i]##*.}" in
					idx|IDX|sub|SUB|sup|SUP|ass|ASS|SSA|SSA|ttxt|TTXT)
					echo
					echo "-> Subtitle format '$(echo ${EXTSUB[$i]##*.} | tr '[:lower:]' '[:upper:]')' not supported by OGM!"
					echo "-> Skipping import of '${EXTSUB[$i]}'"
					EXTSUB[$i]=
					;;
				esac
			fi
		done


Last edited by mutex1; 06-17-2009 at 03:01 PM..
# 2  
Old 06-17-2009

Code:
for n in 1 2 3
do
  var=EXTSUB$n
  eval "val=\$$var"
  case "${val##*.}" in
    ass|ASS|idx|IDX|sub|SUB|sup|SUP|ssa|SSA)
    echo
    echo "-> Subtitle format '${val##*.}' not supported!"
    echo "-> Skipping import..."
    eval "EXTSUB$n="
    ;;
  esac
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Challenge with sh script using environment variables to check for file.

Hi All Thanks for reviewing my question. I have a sh script where I used an environmental variable for the directory for the file I need to check to ensure before executing a process. I have confirmed the permissions and I can find the file if I use a hard coding of the directory. This is a... (5 Replies)
Discussion started by: rstojkovic68
5 Replies

2. UNIX for Beginners Questions & Answers

Question about global environment variables & fork() exec()

Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)
Discussion started by: bodisha
2 Replies

3. UNIX for Dummies Questions & Answers

Check if file is empty with variables, bash

Hello again! I have some trouble with scripting in bash. In the following script I read from a folder with the files line0_Ux.xy line1_Ux.xy line2_Ux.xy . . . Some of the files are empty. For those I would like to print a "0" in list. I think the problem with the code is that... (4 Replies)
Discussion started by: bjoern456
4 Replies

4. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

5. Homework & Coursework Questions

Check 2 variables for matching characters

hi i am writing a hangman script and am having trouble checking the correct letters against the word i need the script to compare the word against the letters guessed that are correct so once all the letters within the word have been guessed it will alow me to create a wining senario this must... (1 Reply)
Discussion started by: lsecer
1 Replies

6. Shell Programming and Scripting

Check whether there is a non printable character in the unix variables

cp $l_options $srcdirfile $destdirfile If i want to check whether there is a non printable character in the variables $l_options $srcdirfile $destdirfile how it can be done? (2 Replies)
Discussion started by: lalitpct
2 Replies

7. Shell Programming and Scripting

How to check exit status of unset variables

Hi All, Is there any way to check exit status of unset variables? In the following code PathX is not set and the script terminates without checking exit status. #!/bin/bash Path="/tmp/log" cd ${PathX:?} if ;then echo "Exit Status : non zero" else echo "Exit Status :... (2 Replies)
Discussion started by: sussus2326
2 Replies

8. Shell Programming and Scripting

Capturing text & putting it in different variables

Dear friends, I have following text in a variable. /backup/rem1/10122010/res From this value of variable i want to take each text in different variables e.g. a=backup b=rem1 c=10122010 d=res please guide me to do so. Thank you in advance Anushree (5 Replies)
Discussion started by: anushree.a
5 Replies

9. Shell Programming and Scripting

Shell Variables & awk...Help Please

I apologize if this topic has been beaten to death here, but my limited searching skills did not throw up any results. Here's what I am trying to accomplish List all the files in a certain directory; assign the file names to an array which will be used later in the script. My script looks like... (2 Replies)
Discussion started by: kash80
2 Replies

10. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies
Login or Register to Ask a Question