LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-14-2012, 09:55 AM   #1
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Rep: Reputation: Disabled
Case statement in function not working


Hi all: At the bottom of this post is the code I cannot get working. What I want to do is pass a variable to the function, which is a three char month (i.e. Jan, Feb, ...). I want the function to return the two digit month number. The case statement works fine if I place it inline, but not as a function. I know I must be passing the parameter improperly (I am just learning BASH scripting).

When I run this code it returns: Dec: command not found

Can someone show me my error? Thanks, Chris.

## Begin code snipet

function conv_file_alpha_month {
case "$file_alpha_month" in
Jan)
echo "01";;
Feb)
echo "02";;
Mar)
echo "03";;
Apr)
echo "04";;
May)
echo "05";;
Jun)
echo "06";;
Jul)
echo "07";;
Aug)
echo "08";;
Sep)
echo "09";;
Oct)
echo "10";;
Nov)
echo "11";;
Dec)
echo "12";;
*)
echo "XX";;
esac
}
file_alpha_month="Dec"
file_digit_month=conv_file_alpha_month $file_alpha_month
 
Old 02-14-2012, 10:04 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
Well, inside the function the first argument should be referenced as $1. In the last line of the script, if you want to assign the output of the conv_file_alpha_month function to a shell variable, you need command substitution:
Code:
file_digit_month=$(conv_file_alpha_month $file_alpha_month)
Hope this helps.
 
Old 02-14-2012, 10:27 AM   #3
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
THANK YOU VERY MUCH. That got it working - almost.

For some reason, the case statement is ALWAYS returning the default "XX". I have tested the code by inserting an echo statement just before the function is called and the file_alpha_month variable is set to Dec, so the case statement should be returning 12.

variable file_alpha_month is set is earlier code. I test for it have the proper value by the echo.

echo %file_alpha_month << this outputs Dec
file_digit_month=conv_file_alpha_month $file_alpha_month

Any suggestions?

Last edited by cmosentine; 02-14-2012 at 10:30 AM.
 
Old 02-14-2012, 10:34 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 9,002

Rep: Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349Reputation: 1349
Can you post the updated version of your script? Please use CODE tags to embed the code: they will improve readability and preserve spacing and indentation (useful to reveal glitches in some circumstances). To use CODE tags, switch to advanced mode and use the # button. Or write them down explicitly as

[CODE]your code here[/CODE]

Thank you.
 
Old 02-14-2012, 10:38 AM   #5
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash

USERNAME="xxxxxxxx"
PASSWORD="xxxxxxxx"
HOSTNAME="xxxxxxxx"

function get_host_file_list {
	ftp_param="ftp://$1:$2@$3"
	ftp -inv $ftp_param << EOF 
	!mkdir /opt/vra/ftp
	ls . /opt/vra/ftp/$HOSTNAME.txt
EOF
}

function conv_file_alpha_month {
	case "%1" in
		Jan)
			echo "01";;
		Feb)
			echo "02";;
		Mar)
			echo "03";;
		Apr)
			echo "04";;
		May)
			echo "05";;
		Jun)
			echo "06";;
		Jul)
			echo "07";;
		Aug)
			echo "08";;
		Sep)
			echo "09";;
		Oct)
			echo "10";;
		Nov)
			echo "11";;
		Dec)
			echo "12";;
		*)
			echo "XX";;
	esac
}

##	This call retrieves 
get_host_file_list $USERNAME $PASSWORD $HOSTNAME

##	The realy work is done in this while loop.
##	Read each line of the file previously created.  Use the data to create the 
##	new file name.  Also, test to see if the destination directory exists, and 
##	if not create it.
while read inputline
do
	file_alpha_month="$(echo $inputline | cut -d " " -f 6)"
	file_day="$(echo $inputline | cut -d " " -f 7)"
	file_year="$(echo $inputline | cut -d " " -f 8)"
	file_name="$(echo $inputline | cut -d " " -f 9)"
	
	echo $file_alpha_month
	file_digit_month=$(conv_file_alpha_month $file_alpha_month)

	new_file_name=$file_year$file_digit_month$file_day"_"$file_name
	dest_directory=/media/nss/NSS1/data/Transcr/$file_year$file_digit_month$file_day
	echo $new_file_name
	echo $dest_directory

done < /opt/vra/ftp/"$HOSTNAME".txt
input file read in while loop
-rwxrwxrwx 1 owner group 4800512 Dec 19 2011 1001.mp3
-rwxrwxrwx 1 owner group 782336 Dec 16 2011 1002.mp3
-rwxrwxrwx 1 owner group 136192 Dec 16 2011 1003.mp3
-rwxrwxrwx 1 owner group 2841600 Dec 15 2011 1004.mp3
-rwxrwxrwx 1 owner group 188416 Dec 16 2011 1005.mp3
-rwxrwxrwx 1 owner group 160768 Dec 15 2011 1006.mp3

Last edited by cmosentine; 02-14-2012 at 10:39 AM.
 
Old 02-14-2012, 10:44 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
case "%1" in should be case "$1" in
 
Old 02-14-2012, 10:49 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
Code:
while read inputline
do
	file_alpha_month="$(echo $inputline | cut -d " " -f 6)"
	file_day="$(echo $inputline | cut -d " " -f 7)"
	file_year="$(echo $inputline | cut -d " " -f 8)"
	file_name="$(echo $inputline | cut -d " " -f 9)"
could be more easily done as
Code:
while read _ _ _ _ _ file_alpha_month file_day file_year file_name _
do
 
Old 02-14-2012, 12:20 PM   #8
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
case "%1" in should be case "$1" in
FRICK! Sometimes I hate programming.
 
Old 02-14-2012, 12:24 PM   #9
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
Code:
while read inputline
do
	file_alpha_month="$(echo $inputline | cut -d " " -f 6)"
	file_day="$(echo $inputline | cut -d " " -f 7)"
	file_year="$(echo $inputline | cut -d " " -f 8)"
	file_name="$(echo $inputline | cut -d " " -f 9)"
could be more easily done as
Code:
while read _ _ _ _ _ file_alpha_month file_day file_year file_name _
do
Thanks. I am just learning BASH so a lot of stuff I do will probably be inefficient. Although, when studying code snippets, BASH can get obfuscated very quickly. The lack of comments in some of the examples from the web does not help.

Onward the upward, Chris.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
Case Statement craigjward Programming 6 12-12-2007 11:33 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
case statement baks Programming 2 03-15-2007 01:19 PM
bash'ed by case statement??? 3inone Programming 2 04-29-2004 04:52 PM


All times are GMT -5. The time now is 03:17 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration