LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 11-09-2009, 06:36 AM   #1
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Rep: Reputation: 15
Script is stripping off leading zero


I have a script which is taking a 10 character variable input by the user. If it begins with a zero, the script unwittingly strips that off, & proceeds with 9 characters. I want to rectify it so either it doesn't do this or if it recognises only nine characters further into the process, adds a zero to the beginning.

The initial syntax calling for the variable is

Code:
echo -n "Enter core-follow date/time for BOC: "
    setenv BOC $<
Had to mod another script to add a zero, but this isn't ideal, if variable doesn't start with zero.

Code:
RETRIEVE FROM FILE "$PAN_MODEL_DIR./data/R$REACTOR..LOADING.0$BOC."
 
Old 11-09-2009, 07:38 AM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
You could try the following to add the leading zeroes back in:

Code:
BOC=`printf "%010s" $BOC`
I'm assuming here that you're talking about a shell script...
 
Old 11-09-2009, 07:51 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
According to the man page on my system, "setenv" is a C function. I do not find it as a command callable in BASH

???
 
Old 11-09-2009, 09:05 AM   #4
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
Yes, this is from a C shell script
 
Old 11-11-2009, 10:42 AM   #5
wtaicken
LQ Newbie
 
Registered: Dec 2008
Location: Dorset, UK
Distribution: Ubuntu 7.1
Posts: 25

Original Poster
Rep: Reputation: 15
Tried the suggestion

Code:
BOC=`printf "%010s" $BOC
`

before the statement

Code:
RETRIEVE FROM FILE "$PAN_MODEL_DIR./data/R$REACTOR..LOADING.$BOC."
but that didn't seem to add a leading zero. Is the syntax correct?
 
Old 11-12-2009, 07:02 AM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Ah, just tested that in the C shell and it hasn't worked:

Code:
$ csh
$ BOC=123456789
BOC=123456789: Command not found.
$ set BOC=123456789
$ echo $BOC
123456789
$ set BOC=`printf "%010s" $BOC`
printf: %010s: invalid conversion specification
$ set BOC=`printf "%010d" $BOC`
$ echo $BOC
0000000000
$
It does work from bash.

Edited to add:
D'Oh! Correction, set BOC=`printf "%010d" $BOC` does work, but only for numeric strings:
Code:
$ set BOC=123456789
$ set BOC=`printf "%010d" $BOC`
$ echo $BOC
0123456789
$ set BOC=12345qrc
$ set BOC=`printf "%010d" $BOC`
printf: 12345qrc: value not completely converted
$ echo $BOC
0000012345

Last edited by Robhogg; 11-12-2009 at 09:12 AM. Reason: Forgot to reset contents of BOC after failed conversion.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stripping lines versus stripping bytes in a bash subshell. poorman_installer Programming 9 10-21-2009 08:36 AM
Stripping down the kernel Oxagast Linux - Kernel 5 06-22-2006 04:23 PM
Chapter 5.33 Stripping LQ496873 Linux From Scratch 2 02-05-2006 09:42 PM
Looking for prelinking + stripping script for root partiton. qwijibow Linux - General 3 09-14-2004 02:19 PM
Stripping down to basics sketchyfrog Linux - Newbie 4 02-24-2004 08:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:40 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration