ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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:
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.
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
#!/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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.