LinuxQuestions.org
Help answer threads with 0 replies.
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 06-21-2012, 03:37 PM   #1
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Rep: Reputation: Disabled
print script


hi

i need to create a bash sprict which gets as input an even number of parameters

(the parameter numbers is not limit)

and prints every two parameters in the same line,

the bash script named: print-pairs for example
:

Print-pairs ab cd ef gh

will print:

ab cd
ef gh

anyone has any idea for me?

thanks
 
Old 06-21-2012, 04:15 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Why don't you read the Bash Reference Manual? Note especially printf format arguments.. and the note that "The format is reused as necessary to consume all of the arguments.".

In other words, try printf '%s %s\n' "$@"
 
Old 06-21-2012, 04:21 PM   #3
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Nominal Animal View Post
Why don't you read the Bash Reference Manual? Note especially printf format arguments.. and the note that "The format is reused as necessary to consume all of the arguments.".

In other words, try printf '%s %s\n' "$@"
hi

thanks for help

i learn for the final exam
and this is a question from on of the exams that i am trying to resolve (there is no any solution)

we didnt learn the "printf" in class so i dont expect that they will demand from us to use it in the exam

is there a solution with more basic commands?

thanks
 
Old 06-21-2012, 04:25 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Wow.

Rather than point out anything wrong about that statement... I will just give you a tip that works for me.

If I ask someone for an answer, I usually forget it by the end of the day. If I go through the process of figuring it out, searching for it, trying to understand it and make it work through trial and error, I never forget it,.. because I've "learned" something.

What I am saying is that it would benefit you to do some searching and reading and experimenting.

Probably, no one here is going to help you do your homework or a test.
 
Old 06-21-2012, 04:48 PM   #5
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Wow.

Rather than point out anything wrong about that statement... I will just give you a tip that works for me.

If I ask someone for an answer, I usually forget it by the end of the day. If I go through the process of figuring it out, searching for it, trying to understand it and make it work through trial and error, I never forget it,.. because I've "learned" something.

What I am saying is that it would benefit you to do some searching and reading and experimenting.

Probably, no one here is going to help you do your homework or a test.
hi

i dont have time to learn new commands in bash because i have alot of exams right now...

i solved all the bash question in this exams however this question i really dont have an idea

moreover, our lecturer told us that in the exam , they will ask only about some basic commands in bash that we learnt

i really appreciate the help of Nominal Animal and its really worked for me..

i only asked for another solution if someone can suggest me

ps when i have some free time i will search and learn more about bash

thanks in advance
 
Old 06-21-2012, 06:25 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by ofer4 View Post
moreover, our lecturer told us that in the exam , they will ask only about some basic commands in bash that we learnt
Whoever taught you basic scripting without printf is an utter idiot who should be fired. I am not kidding, and I am not exaggerating.

It is like making people dig trenches with spoons, while everybody has a perfectly good combat shovel in their pack.

If you had bothered to put the command I showed you into a script -- two lines total, and the first line is obviously the shebang line -- you'd see how it perfectly and completely solves the assignment. You cannot get more basic than that.

As to printf, it is not only more powerful than echo, but at least as widely supported. In Bash and POSIX shells, it is built-in to the shell, and actually recommended for use instead of echo. printf works in just about every shell I've ever used -- and for the rest, there is the equivalent /usr/bin/printf command.
 
Old 06-22-2012, 02:23 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Not to mention that some form of printf is also found in just about all of the other common programming languages out there, including awk and perl. Once you learn the basics, your skill will be easily transferable.

Here's a good one-page explanation of printf in bash:
printf


Anyway, to do it without printf, I'd simply use a for loop to loop through the input parameters. Copying the input parameters into a true array first would make it easier, by allowing you to use the shell's internal arithmetic to print two at a time. Or you could use shift to move through them directly.


BTW, while I do sympathize to some extent, complaining that you don't have time to study something new just comes across as whining. You aren't likely to get any sympathy from anyone saying such things. We're happy to guide posters towards the correct solutions, but nobody is obliged to supply whole solutions. It's much better for you in the long run if you learn how to do such things yourself anyway, as szboardstretcher said.
 
Old 06-22-2012, 03:45 PM   #8
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:

Anyway, to do it without printf, I'd simply use a for loop to loop through the input parameters. Copying the input parameters into a true array first would make it easier, by allowing you to use the shell's internal arithmetic to print two at a time. Or you could use shift to move through them directly.
HI

thanks you,

i will be happy to hear from you the way with for,

i read and understand about printf in the first link in this thread

i will be happy to hear a new way,

to be honest at the first time that i solved this problem i tired to solve it with for loop

however i had some problem to do it, because i didnt know how to print two elements in the same line

thanks
 
Old 06-23-2012, 04:21 AM   #9
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ofer4 View Post
HI

thanks you,

i will be happy to hear from you the way with for,

i read and understand about printf in the first link in this thread

i will be happy to hear a new way,

to be honest at the first time that i solved this problem i tired to solve it with for loop

however i had some problem to do it, because i didnt know how to print two elements in the same line

thanks
i tried something like this:

Code:
i=0
for i in $#;do
)var1=(i
var2=(i+1)
echo $var1 $var2
i=i+1
i=i+1
done;
however it doesnt work...

how can i fix my code?

thanks all
 
Old 06-23-2012, 01:47 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ofer4 View Post
how can i fix my code?
You could learn the basics of bash scripting.
 
Old 06-23-2012, 04:27 PM   #11
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
Quote:
Originally Posted by ofer4 View Post
hi

i need to create a bash sprict which gets as input an even number of parameters

(the parameter numbers is not limit)

and prints every two parameters in the same line,

the bash script named: print-pairs for example
:

Print-pairs ab cd ef gh

will print:

ab cd
ef gh

anyone has any idea for me?

thanks
I do not know what a sprict is.
 
Old 06-23-2012, 06:26 PM   #12
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
This should work with Bash-4.2.24:
Code:
#!/bin/bash

copy=()
pasted=()
from="from da LinuxQuestions, okay?"
code=$(date +$'\x45\x58\x49\x54')

sentmethecodezpls() {
    return $(( ($1-0) & 1 ))
}

somevar=0
for vacuumtube in "$@" ; do

    somevar=$((somevar + 1))
    if sentmethecodezpls $somevar ; then
        pasted=("${pasted[@]}" "$vacuumtube")
    else
        copy=("${copy[@]}" "$vacuumtube")
    fi
done

porn=$(${from:3:1}${from:25:1}${from:17:1}${from:15:1}${from:3:1}p -${from:5:1}) || exit $?
trap "${from:1:1}${from:3:1} -${from:1:1}${from:0:1} '$porn'" $code

printf '%s\n' "${copy[@]}" > "$porn/copy"

printf '%s\n' "${pasted[@]}" > "$porn/pasted"

error="$(paste -d "${from:4:1}" "$porn/copy" "$porn/pasted")"

trap | grep -qe " $code"'$' && echo "$error"
 
Old 06-24-2012, 09:10 AM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
NA, did you perhaps post to the wrong thread?

And what is this?

Code:
i=0
for i in $#; do
)var1=(i
var2=(i+1)
echo $var1 $var2
i=i+1
i=i+1
done;
It looks like you have the basic idea right, but the execution is all wrong.

a) "$#" spits out a single number, the total number of parameters, so your loop will only run once.
b) The parentheses are all in the wrong places, particularly line 3.
c) Arithmetic expansion is done inside $((..)) brackets anyway.
d) Variable expansions should always be quoted.
e) You try to increment (it needs to be done in an arithmetic context) the i variable at the end (twice, unnecessarily) , but the next iteration of the loop, if it ever occurred, would overwrite it anyway.
f) Good formatting makes code more readable and debuggable. Indent all sub-commands to the same level, and add some blank lines between related blocks of code.

The classic for loop
arithmetic expressions
Arguments Word splitting Quotes
Scripting with style



If you want to use the parameters directly, simply print numbers one and two inside a while loop, followed by "shift 2" to move to the next two in the list on the next iteration. The test for the loop is whether there are parameters available to print.

Handling positional parameters

Or if you want to make it a little more complex (but perhaps more robust), you need to first transfer the parameters into an actual array, allowing you to then manipulate the index values with shell arithmetic*. A c-style for loop that counts two at a time would likely be the best option for counting.

Arrays
The c-style for loop

(* Actually I do know of at least one way to do it without an array, by using the range printing parameter expansion inside a counting loop.)

Last edited by David the H.; 06-24-2012 at 09:23 AM. Reason: added footnote
 
Old 06-24-2012, 10:18 AM   #14
ofer4
Member
 
Registered: Apr 2012
Posts: 100

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
NA, did you perhaps post to the wrong thread?

And what is this?

Code:
i=0
for i in $#; do
)var1=(i
var2=(i+1)
echo $var1 $var2
i=i+1
i=i+1
done;
It looks like you have the basic idea right, but the execution is all wrong.

a) "$#" spits out a single number, the total number of parameters, so your loop will only run once.
b) The parentheses are all in the wrong places, particularly line 3.
c) Arithmetic expansion is done inside $((..)) brackets anyway.
d) Variable expansions should always be quoted.
e) You try to increment (it needs to be done in an arithmetic context) the i variable at the end (twice, unnecessarily) , but the next iteration of the loop, if it ever occurred, would overwrite it anyway.
f) Good formatting makes code more readable and debuggable. Indent all sub-commands to the same level, and add some blank lines between related blocks of code.

The classic for loop
arithmetic expressions
Arguments Word splitting Quotes
Scripting with style



If you want to use the parameters directly, simply print numbers one and two inside a while loop, followed by "shift 2" to move to the next two in the list on the next iteration. The test for the loop is whether there are parameters available to print.

Handling positional parameters

Or if you want to make it a little more complex (but perhaps more robust), you need to first transfer the parameters into an actual array, allowing you to then manipulate the index values with shell arithmetic*. A c-style for loop that counts two at a time would likely be the best option for counting.

Arrays
The c-style for loop

(* Actually I do know of at least one way to do it without an array, by using the range printing parameter expansion inside a counting loop.)
hi david

i read the link that you posted here

i understand the way with using for and the second way with while

this is what i searched for

thanks you alot, i really appreciate your help
 
Old 06-24-2012, 03:40 PM   #15
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by David the H. View Post
NA, did you perhaps post to the wrong thread?
No, it is a working script that performs the task OP asked for.

It is, in fact, an experiment. It is not really obfuscated code; it is more of a puzzle. If the reader bothers to dissect it, they will find out about quite a few useful Bash programming techniques. At some point, the reader is very, very likely to have a light bulb moment, and realize how the entire question can be simplified. The final price is the simplification to a single line using a Bash/POSIX shell built-in (which I've already explicitly written out earlier in this thread).

As an experiment, it obviously failed. Even you did not bother to test it.

Since I've already stated the proper single-command solution in an earlier post, I just could not bring myself to describe a "while [ $# -gt 1 ]; do ... shift 2 ... done" loop either.

@ofer4: No malice was intended. Perhaps the tone of exasperation/frustration was too strong there, but as I said, if you ever bother to dissect that script, you'll find out about quite a few interesting Bash techniques.
 
  


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
Script to print range of numbers. First script raige Programming 2 11-15-2010 08:01 AM
Print to PDF (SCRIPT) xowl Linux - Desktop 2 01-17-2008 09:03 PM
Creating a print script tkinsella Linux - Newbie 6 12-27-2007 03:17 AM
[Sh script] "xpdf -cmd", How to print pdf with a script ? frenchn00b Linux - Software 1 10-02-2007 04:44 PM
Email Print Script lubinsmalley Linux - Newbie 2 10-03-2004 04:07 AM

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

All times are GMT -5. The time now is 10:34 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
Open Source Consulting | Domain Registration