How to accept command line argument as character or text if number is entered?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to accept command line argument as character or text if number is entered?
# 1  
Old 06-10-2014
How to accept command line argument as character or text if number is entered?

Hello
Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex
# 2  
Old 06-10-2014
Take a look at the bc command.

Code:
echo "obase=16; 10"|bc

# 3  
Old 06-10-2014
Or:
Code:
$ i=300
$ printf "%X\n" "$i"
12C

# 4  
Old 06-10-2014
My question is in command line if i enter 8016 , it will display output as 38 30 31 36 by storing it in some variable

Last edited by aadityapatel198; 06-10-2014 at 06:18 PM..
# 5  
Old 06-10-2014
Longhand using OSX 10.7.5 default bash terminal.
Code:
#!/bin/ksh
printf "$1" > /tmp/text
hexdump -v -e '1/1 "%X "' < /tmp/text

Results:-
Code:
Last login: Tue Jun 10 22:20:53 on ttys000
AMIGA:barrywalker~> ./arg_num.sh 1273455667
31 32 37 33 34 35 35 36 36 37 AMIGA:barrywalker~> _

# 6  
Old 06-10-2014
Thanks for the reply my last question how i will store hex 31 32 37 33 34 35 35 36 36 37 value of characters 1273455667 in some variable for further manipulation
# 7  
Old 06-10-2014
Same machine...
Code:
#!/bin/ksh
printf "$1" > /tmp/text
string=`hexdump -v -e '1/1 "%X "' < /tmp/text`
echo "$string"

Results:-
Code:
Last login: Tue Jun 10 22:24:16 on ttys000
AMIGA:barrywalker~> ./arg_num.sh llaksdlkjasdlkas
6C 6C 61 6B 73 64 6C 6B 6A 61 73 64 6C 6B 61 73 
AMIGA:barrywalker~> ./arg_num.sh 1273455667
31 32 37 33 34 35 35 36 36 37 
AMIGA:barrywalker~> _

HTH
This User Gave Thanks to wisecracker 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

How to pass each line of a text file as an argument to a command?

I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. I am fetching object files from a library file, I have all the object file... (2 Replies)
Discussion started by: Paul Martins
2 Replies

2. Homework & Coursework Questions

Make a file accept only two arguments from the command line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1) The script is executed in the Korn shell. 2) Name the shell script file is asg6s. 3) The asg6s file is... (7 Replies)
Discussion started by: ProgMan2015
7 Replies

3. Programming

Make a file accept only two arguments from the command line

DELETED (2 Replies)
Discussion started by: ProgMan2015
2 Replies

4. Shell Programming and Scripting

Perl : accept multiple user entered command line arguemnts

I am trying to create a script that will accept multi input from the user (really just me), then execute those command on a remote device. My question is if the I enter "No" at the confirmation point "Are these statements correct y or n ?", what is the best way to go back and start over ? I... (3 Replies)
Discussion started by: popeye
3 Replies

5. Shell Programming and Scripting

How to loop through array who's name is entered in command line?

Say I have a ksh program called test.ksh which has several defined arrays inside it such as array1,array2,array3...These arrays contain strings. I also have a method in the program: for x in $1 do ....(#do something) done So, when the user enteres: ./test.ksh array1, I want to... (3 Replies)
Discussion started by: mrskittles99
3 Replies

6. Shell Programming and Scripting

only a number can be entered no letters?

ok the user can only enter a number if a letter is entered it shouldnt be accepted This is what i have so far read -p "How many cars to enter:" cars until do read -p "Invalid number. Please re-enter:" $tags done (5 Replies)
Discussion started by: gangsta
5 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

9. UNIX for Dummies Questions & Answers

Append 0 for single digit entered from command line

I have a script like this-- #!/bin/ksh echo "To pad a 0 before digits from 1-9" for i in $* do echo $i | sed 's//'0'/g' done I run this script as ksh name 1 2 23 34 The output should be 01 02 23 34 Help me in modifying this script. Thanks Namish (2 Replies)
Discussion started by: namishtiwari
2 Replies

10. Shell Programming and Scripting

getting rid of $ when entered at the command line

Hi everyone, Can someone possibly help me with this problem I am having please. I wrote a Korn shell script to manipulate currency amounts in a way that a person could use this script to determine the minimum number of coins required to make a certain amount. for example when entered on the... (2 Replies)
Discussion started by: bashirpopal
2 Replies
Login or Register to Ask a Question