LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple bash script programming problem (https://www.linuxquestions.org/questions/programming-9/simple-bash-script-programming-problem-616869/)

ArthurHuang 01-28-2008 09:33 AM

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!

unSpawn 01-28-2008 09:45 AM

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"; }


ArthurHuang 01-28-2008 10:06 AM

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 (Post 3037677)
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"; }



unSpawn 01-28-2008 10:53 AM

in this specific case you could have
Code:

pattern="ftp=(1012, 2345)"
pattern="${pattern//[a-z,=\(\)]/}"

if you played with the examples.


All times are GMT -5. The time now is 04:37 PM.