Bash variable not being passed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash variable not being passed
# 1  
Old 12-17-2015
Bash variable not being passed

In the bash below the variable date displays in the echo. However when I use it in the for loop it does not. Basically, the user inputs a date then that date is converted to the desired format of (month-day-year, no leading 0). That input is used in the for loop to return every file that matches the .bam extension. I can not seem to figure out why the $date variable is not being passed to the path to search in? Thank you Smilie.

Code:
printf "\n\n"
printf "What is the date to analyze : "; read id
    date -d "$id" +%-m-%-d-%Y ; read date #convert to month-date-year
    echo "$date"
    [ -z "$id" ] && printf "\n No date supplied. Leaving match function." && sleep 2 && return
        for f in /home/cmccabe/Desktop/NGS/bam/$date/*.bam ; do
        bname=`basename $f`
        pref=${bname%%.bam}
        echo "The bam files found in directory at $f are ${pref}"
done

# 2  
Old 12-17-2015
Did you enter a value to be read into the date variable?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-17-2015
Yes, I entered 10/29/2015 and echo shows 10-29-2015, which is correct and is the variable $date. Also, if I hard code 10-29-2015 into the path it seems to work. Thank you Smilie.

Last edited by cmccabe; 12-17-2015 at 03:44 PM.. Reason: fixed format
# 4  
Old 12-17-2015
Instead of this:
Code:
date -d "$id" +%-m-%-d-%Y ; read date #convert to month-date-year

Wouldn't this be what you want?
Code:
date=$(date -d "$id" "+%m-%d-%Y")

This User Gave Thanks to Aia For This Post:
# 5  
Old 12-17-2015
Thank you @Aia that was it Smilie
# 6  
Old 12-17-2015
By the way, you can avoid calling an outside program to give you just the name of the file, which is more expensive.
Instead of bname=`basename $f` change to bname=${f##*/}
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable not passed to the sed command

Hello, I am writing a script which is not giving the desired result. When I check the content of the 'InputFile_009_0.sh', it shows following with missing Index in this command sed -i "s/L1ITMBLT.root/L1ITMBLT_"".root/g" run_DttfFromCombinedPrimitives_cfg.py of . Any help? ... (13 Replies)
Discussion started by: emily
13 Replies

2. Shell Programming and Scripting

Multiple bash variables passed into nawk

I have a file that has 2 fields called b_file: 11977 DAR.V3.20150209.1.CSV 3295 DAR.V3.20150209.1.CSV 1721 DAR.V2.20150210.1.CSV I need to search a sftplog using the field 1, but want to maintain the relationship between field 1 and 2. I am passing field 1 as a parameter in a bash loop. ... (14 Replies)
Discussion started by: smenago
14 Replies

3. Shell Programming and Scripting

[BASH] getopts, OPTARG is passed but empty

EDIT: -- SOLVED -- Heyas, Getting used to optargs, but by far not understanding it. So i have that script that shall be 'changeable', trying to use the passed arguments to modify the script visuals. Passing: browser -t test -d sect $HOME Where -t should change the title, and -d... (0 Replies)
Discussion started by: sea
0 Replies

4. Shell Programming and Scripting

Variable passed as argument

I have a script. #!/bin/sh cur_$1_modify_time=Hello echo "cur_$1_modify_time" When I run like sh /root/script1 jj I expect value "Hello" being assigned to variable "cur_jj_modify_time" and output being "Hello" ie echoing $cur_jj_modify_time But the output comes as # sh... (3 Replies)
Discussion started by: anil510
3 Replies

5. Shell Programming and Scripting

In the sh file variable is not being passed on.

I am having difficulties with the fllowing script: !/bin/sh voicemaildir=/var/spool/asterisk/voicemail/$1/$2/INBOX/ echo `date` ':' $voicemaildir >> /var/log/voicemail-notify.log for audiofile in `ls $voicemaildir/*.wav`; do transcriptfile=${audiofile/wav/transcript} ... (4 Replies)
Discussion started by: ghurty
4 Replies

6. Shell Programming and Scripting

My variable cannot be passed through into my path

Hi, Can you please help. I am scripting in sh and I am trying to simply copy one directory to another but for some reason my variables are not recognised? echo "The latest version of the program is being found......." cd $SOFTWARE/src/$progname version=`ls $SOFTWARE/src/$progname | grep... (13 Replies)
Discussion started by: cyberfrog
13 Replies

7. Shell Programming and Scripting

bash array passed to oracle

Hi all.. Does anyone know have an example of passing the contents of a bash array to oracle? basically I am looking to loop through the contents of a file(not csv!) and store each line into a bash array. Once i have this I can then pass the array into an oracle procedure that accepts an array... (1 Reply)
Discussion started by: satnamx
1 Replies

8. UNIX for Dummies Questions & Answers

Assigning a Variable all the Parameters passed

Hi, I have a unix script which can accept n number of parameters . I can get the parameter count using the following command and assign it to a variable file_count=$# Is there a similar command through which i can assign a variable all the values that i have passed as a parameter ... (2 Replies)
Discussion started by: samit_9999
2 Replies

9. UNIX for Dummies Questions & Answers

variable passed to awk

Does anybody know how to print a variable passed to awk command? awk -F"|" 'BEGIN {print $job,"\n","Question \n"} {print $1,$2$4," ",$3}' "job=$job1" file1 I am trying to pass job the variable job1. the output is blank. ?? (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Dummies Questions & Answers

variable passed to awk

Anybody know what's wrong with this syntax? awk -v job="$job" 'BEGIN { FS="|"} {print $1,$2," ",$4," ",$3\n,$5,"\n"}' list It's keeping give me this message: awk: syntax error near line 1 awk: bailing out near line 1 It seems awk has problem with my BEGIN command. Any... (8 Replies)
Discussion started by: whatisthis
8 Replies
Login or Register to Ask a Question