ERP - MES Interface - several function | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

ERP - MES Interface - several function

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-02-2013
Noobie1995 Noobie1995 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 14 March 2013, 10:10 AM EDT
Location: Berlin (close to it), GER
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
Bug ERP - MES Interface - several function

Hi all,

I am a rookie regarding shell script programming, YET!! :-)

Hence, I have choosen a problem from my bad side of life (my job) and I wil try to solve it by using a shell script and get knowledge concerning shell script programming.

Question: As you can see I have already figured out how to use a function, variables, different standard funtions (if, loops,etc). But I struggeld by using the printf function in order to add blanks to string. If the string which I pass into the function is longer than 40 digits then the printf function has to shorten string to 40 digits. This function works perfectly but if the string is shorter than 40 digits then the printf function has to add blanks until the string has a lenght of 40 digits but this part of my shell script doesn't work !!! The function adds only ONE blank


Code:
#!/bin/bash
fkt_auffuellen()
{
case $1 in
 "C")
   if [ ${#2} -eq $3 ]; then
    value=$2
   elif [ ${#2} -lt $3 ]; then 
    value="$(printf "%-$3s" $2)"
   else
    value="$(printf "%.$3s" $2)"
fi;; 
 "N")
;;
 "D")
;;
esac
}
vorg_nr_t=C
vorg_nr="AUFTRAG005-010010"
vorg_nr_l=40
fkt_auffuellen $vorg_nr_t $vorg_nr $vorg_nr_l
echo $value

if anyone can give an advice it would be great!!!!

Thanks in advance and cold but sunny greetings form BERLIN Area!

Noobie1995

P.S.: I am so sorry for my english it has been a while..........
Sponsored Links
    #2  
Old 03-02-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 November 2014, 3:31 PM EST
Location: Amsterdam
Posts: 9,623
Thanks: 294
Thanked 2,455 Times in 2,203 Posts
Hi, try:

Code:
echo "$value"

The Following User Says Thank You to Scrutinizer For This Useful Post:
Noobie1995 (03-02-2013)
Sponsored Links
    #3  
Old 03-02-2013
Noobie1995 Noobie1995 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 14 March 2013, 10:10 AM EDT
Location: Berlin (close to it), GER
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
Great, it works but you can imagine it brings me to some more questions:
1. why I have to pass the value variable into ""?
2. I have extended the code (green lines) and the code brings the previous[incorrect result]?


Code:
#!/bin/bash
fkt_auffuellen()
{
case $1 in
 "C")
   if [ ${#2} -eq $3 ]; then
    value=$2
   elif [ ${#2} -lt $3 ]; then 
    value="$(printf "%-$3s" $2)"
   else
    value="$(printf "%.$3s" $2)"
fi;; 
 "N")
;;
 "D")
;;
esac
}
vorg_nr_t=C
vorg_nr="AUFTRAG005-010010"
vorg_nr_l=40
fkt_auffuellen $vorg_nr_t $vorg_nr $vorg_nr_l
vorg_nr="$value"
echo $vorg_nr

    #4  
Old 03-02-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 November 2014, 3:31 PM EST
Location: Amsterdam
Posts: 9,623
Thanks: 294
Thanked 2,455 Times in 2,203 Posts
Try:

Code:
vorg_nr=$value
echo "$vorg_nr"

You do not need the double quotes with the assignment, but you do need them with the echo statement, otherwise the content of the variable will be interpreted by the shell and split into fields using IFS (the input field selector)..
The Following User Says Thank You to Scrutinizer For This Useful Post:
Noobie1995 (03-02-2013)
Sponsored Links
    #5  
Old 03-02-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 22 November 2014, 10:54 PM EST
Location: Simplicity
Posts: 2,053
Thanks: 134
Thanked 603 Times in 547 Posts
And the same rule applies here:

Code:
value=$(printf "%-$3s" "$2")

value= is an assignment, no need for quotes.
$2 is an argument (of the printf command), to be quoted.

---------- Post updated at 12:37 PM ---------- Previous update was at 12:27 PM ----------

The exception proves the rule:

Code:
if [ ${#2} -eq "$3" ]; then

These are arguments to the [ ] (test) function.
The $3 should be quoted if can be tampered with, e.g. became a * or 1 2 .
Not needed, if your script guarantees a number.
The ${#2} is always a number.
The Following User Says Thank You to MadeInGermany For This Useful Post:
Noobie1995 (03-02-2013)
Sponsored Links
    #6  
Old 03-02-2013
Noobie1995 Noobie1995 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 14 March 2013, 10:10 AM EDT
Location: Berlin (close to it), GER
Posts: 8
Thanks: 7
Thanked 0 Times in 0 Posts
Thanks all,

the comments have been very helpful and I can see clearly the problem.

Thanks and have a nice weekend

BR,

Noobie1995
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
CK-ERP (Open Source Edu / ERP / CRM / MRP) v.0.30.1 released chiukay UNIX and Linux Applications 0 09-28-2010 04:13 AM
Compilation problem with Posix Mes Q kumaran_5555 Programming 4 05-19-2010 06:54 AM
Command 'rm -f -r "0yfOYy-0008Nq-2j-32233-K"' failed with return code 1 and error mes linuxbee UNIX for Dummies Questions & Answers 3 05-14-2009 05:00 PM
Linux based Open Source ERP System - CK-ERP chiukay UNIX and Linux Applications 0 06-25-2008 03:46 AM



All times are GMT -4. The time now is 04:41 PM.