[Solved] Problem insert $1 value into a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Problem insert $1 value into a command
# 1  
Old 09-30-2011
[Solved] Problem insert $1 value into a command

Hi folks

I am trying to use a call to a python command within a shell script to help validate a date. If I hard code the date to be checked in the string it works. As follows

Code:
 
cmd_result=$(/usr/bin/python -c 'import time; print time.strftime("%d",time.strptime("31/02/2008", "%d/%m/%Y"))' \
 2>/dev/null | wc -l)
if [ ${cmd_result} -eq 0 ]; then
    echo "Date Invalid "
else
    echo "Date Valid"
fi

If i use $1 instead, i.e. passing the date as an input paramater (e.g ./sysReport.sh 23/04/2011), I cant seem to get the value in $1 to be picked up by the command. I am using the following.

Code:
 
datein=$1
cmd_result=$(/usr/bin/python -c "import time; print time.strftime('%d',time.strptime('" ${datein} "', '%d/%m/%Y'))" \
 2>/dev/null | wc -l)
if [ ${cmd_result} -eq 0 ]; then
    echo "Date Invalid "
else
    echo "Date Valid"
fi

I know that datein stores the value of the input parameter $1 but the execution of the python command always returns zero for valid or invalid dates.

I kow I am not using the reference to the ${datein} or $1 varaiable correctly but have tried numerous mechanisms without any joy.

I can easily just some code from the web that validates a date but this is more about why i cant seem to execute the python command and why i cant insert the contents of a variable into the command.

Any help explaining this or helping to find a solution would be appreciated.
# 2  
Old 09-30-2011
Remove spaces:
Code:
'"${datein}"'

This User Gave Thanks to yazu For This Post:
# 3  
Old 09-30-2011
Yazu,

thanks for the spot. It helped but the overall problem manifested itself with how the shell was handling ' and " characters.

not long at shell programming but found out that i can turn on debug (set -x) and it dumped the exact command and I then spotted the confusion.

The correct syntax is as follows:

Code:
 
cmd_result=$(/usr/bin/python -c "import time; print time.strftime(\"%d\",time.strptime(""\"${datein}\""", \"%d/%m/%Y\"))" \
 2>/dev/null | wc -l)

Many thanks to Yazu for replying and anyone else for checking the post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Insert tabs as delimiter

Hello all, I have an unstructured file with space as delimiter , which I want to structure. The output file should actually have only 5 columns with tab as delimiter. The 4th column can have only 3 values ( biological_process , cellular_component , molecular_function ) Here is how the... (12 Replies)
Discussion started by: ritakadm
12 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Problem With "xhost +" command-FreeBSD

Does anyone no why "xhost +" would not be working in freebsd? Could it be to do with a certain xorg package not being installed etc.. I am getting error "xhost: command not found" Thanks Anyone (2 Replies)
Discussion started by: Browser
2 Replies

3. Shell Programming and Scripting

[Solved] Problem with if-then-else loop

Hi, i have a problem with this script: for i in $(cat list_ip_switch) do if if ; then echo "found ip" else echo "not found ip" fi done cat list_ip_switch 10.155.249.171 10.155.249.172 (3 Replies)
Discussion started by: elilmal
3 Replies

4. UNIX for Advanced & Expert Users

Insert Command Creating Table Locking Problem

Hi, i have a java based tool which does insert operation in a TABLE, and in parallel the same table is used by my C++ code which does select Query. the Table will be always busy, but sometimes the table is getting locked when i try to make an insert, am bit confused whether the lock is... (9 Replies)
Discussion started by: senkerth
9 Replies

5. Solaris

Execution problem in shell script while insert into DB

Hi, am facing some problem while inserting a record into a script Please find script below. `sqlplus -s asdf/asdf123 <<eof! set feedback off; set heading off; set verify off; insert into... (2 Replies)
Discussion started by: senkerth
2 Replies

6. UNIX for Dummies Questions & Answers

[Solved] execution problem

Hi I want to make a script . In this script i want to use input file and this input file consist of three numbers in a line for example input file is as below: 919876543210 09876543234567876 98764534245678 aircelmms","aircelweb","aircelwap" 096574235625... (2 Replies)
Discussion started by: esumiba
2 Replies

7. UNIX for Advanced & Expert Users

unix command : how to insert text at the cursor location via command line?

Hi, Well my title isn't very clear I think. So to understand my goal: I have a script "test1" #!/bin/bash xvkbd -text blabla with xbindkeys, I bind F5 key in order it runs my test1 script So when I press F5, test1 runs. I'm under Emacs/Vi and I press F5 in order to have "blabla" be... (0 Replies)
Discussion started by: xib.be
0 Replies

8. OS X (Apple)

keyboard problem how to insert --> {} \

I have MACOSX Leoparad and MacBook Pro but I am unable to insert the above letters when I am in terminal any help please? e.g. to insert them I need to press alt+shift+) == } but if I do so pressing alt+shift+) nothing works :( ---------- Post updated at 03:54 AM ---------- Previous update was... (2 Replies)
Discussion started by: c_lady
2 Replies

9. AIX

Problem Solved

Generally, most people, I guess, go from 5.3 ML4 Directly to TL 7. So they may never run into this issue. For the rest of us, here is the resolution of my problem in going from ML6 to TL7. Apparently with the change from ML to TL IBM added a "BuildDate Verification" routine into... (1 Reply)
Discussion started by: mrmurdock
1 Replies

10. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies
Login or Register to Ask a Question