LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 01-28-2008, 10:33 AM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Simple bash script programming problem


I am very new to script programming...
I want to read two parameters 1012 and 2345 from pattern

ftp=(1012,2345)

How can I make it?

Thanks!
 
Old 01-28-2008, 10:45 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,743
Blog Entries: 54

Rep: Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973
Code:
s="I want to read two parameters 1012 and 2345 from pattern"

# 0. fixed field, bash:
f=(${s}); ftp="${f[6]} ${f[8]}"

# 1. fixed field, +awk:
echo "$s"|awk '{print $7, $9}'

# 2. search fields, bash:
f=(${s}); for n in `seq 0 ${#f[@]}`; do
 case "${f[$n]}" in [0-9]*) echo "${f[$n]}";; esac
done

# 3. replacement, the wrong way ;-p
f="${s//[a-z]/}"
It all depends on where and how you want to use it.



Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
 
Old 01-28-2008, 11:06 AM   #3
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Original Poster
Rep: Reputation: 30
Wow...the answer is too much to me.
Actually, I just want to get number 1012 and 2345 from pattern
ftp=(1012, 2345), which is fixed...

Thank you again
Quote:
Originally Posted by unSpawn View Post
Code:
s="I want to read two parameters 1012 and 2345 from pattern"

# 0. fixed field, bash:
f=(${s}); ftp="${f[6]} ${f[8]}"

# 1. fixed field, +awk:
echo "$s"|awk '{print $7, $9}'

# 2. search fields, bash:
f=(${s}); for n in `seq 0 ${#f[@]}`; do
 case "${f[$n]}" in [0-9]*) echo "${f[$n]}";; esac
done

# 3. replacement, the wrong way ;-p
f="${s//[a-z]/}"
It all depends on where and how you want to use it.



Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
 
Old 01-28-2008, 11:53 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,743
Blog Entries: 54

Rep: Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973Reputation: 2973
in this specific case you could have
Code:
pattern="ftp=(1012, 2345)"
pattern="${pattern//[a-z,=\(\)]/}"
if you played with the examples.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script programming problem ArthurHuang Programming 5 01-23-2008 04:24 PM
Idiotically simple bash script problem. morrolan Programming 7 01-14-2006 06:09 PM
very simple bash programming question!? Thinking Programming 7 06-01-2005 12:07 PM
problem with a simple script bash stockerz Linux - Newbie 1 09-21-2004 12:42 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM


All times are GMT -5. The time now is 08:48 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration