Is there a way to maybe run these two commands as one?

None-yet

Member
Credits
644
I need to run a couple things in the command line this morning. First I need to run
Code:
 sort -u --merge --parallel=4 1.txt 2.txt 3.txt 4.txt 5.txt 6.txt 7.txt 8.txt 9.txt 10.txt 11.txt 12.txt 13.txt 14.txt 15.txt 16.txt 17.txt 19.txt 20.txt 21.txt 22.txt 23.txt 24.txt>combined-9.9.txt
and then
Code:
 sed 's/^/example    /' combined-9.9.txt> 9-9-complete.txt
. However I got to thinking maybe there is a way to put these together. I have been unable to find a way to join these or something like it that will still provide the same results. I there a way to combine these?
 


Nelson Muntz

Active Member
Credits
1,707
I don't believe it's "languish specific".

It's all I've ever used and has never not worked on running 2 commands at the same time.

I've never tried it with more than 2 commands so uncertain if it would run 3 or more commands at the same time.
 

Vrai

Well-Known Member
Credits
2,592
May I ask. Is the "&&" to be used any time you need to join one or more commands or is it languish specific?
I don't think it actually "joins" the commands. I believe what "&&" does is run the following command as soon as the preceding command is finished.

I don't know for certain it it will work on multiple commands but I don't see why it would not.
(Time to run an experiment!)
 

World

New Member
Credits
37
I don't think it actually "joins" the commands. I believe what "&&" does is run the following command as soon as the preceding command is finished.
That's right, it's sequential. But "joins" is a good word. In geek-speak you also find it called "concatenate." I like to keep commands on separate lines, ending with &&. It helps for troubleshooting. I think a copy/paste of the code below will work in a terminal. You have to enter your own value for "name."

Code:
echo "Hello World!" &&
echo "Hello! What is your name?" &&
read name &&
echo "Thank you, $name. Have a good day!"
 
Last edited:

JasKinasis

Well-Known Member
Credits
2,928
Just to be clear on this - YES, you can join two or more commands together using the && (AND) operator.

But you should be aware that it is a conditional operation.
The operations are processed from left to right - BUT - the right-hand command after an && will ONLY be executed if the command to its left succeeds.

And in a Unix-like system, "success" is indicated by a return-value of 0. Any value other than 0 indicates failure.

So the command to the right of an && operator only gets executed if the command to the left returns 0.

Bearing that in mind - consider this:
Bash:
echo "This prints!" && echo "So does this!" && false && echo "What about this?!"
Which yields the following output:
Code:
This prints!
So does this!
The final string "What about this?" is not shown....
Why is this? All of the commands were chained together using &&......

Let's see:
The first command echo's to the screen, returns 0 - fulfilling the criteria for the &&/AND operator - so the second command is executed.
The second command echo's to the screen, returns 0 - and the third command is executed.
The third command is false (which always returns 1, or false) so because of this - the final echo command does not get ran.

So - yes, you can combine commands using &&, but it is a conditional operator.


Similarly, there is the || (OR) operator.
With the OR operator, the operation on the right will only be ran if the operation on the left fails.
e.g.
Bash:
echo "Hello!" || echo "Goodbye!"
Yields:
Hello!

Now let's simulate the first call to echo failing by calling false instead, just so you can see it working:
Bash:
false || echo "Goodbye!"
Yields:
Goodbye!

Now, that might not seem particularly useful in those last two examples. However, you could use || (OR) to catch the failure of false in my original snippet and print something else:
e.g.
Code:
echo "This prints!" && echo "So does this!" && false && echo "What about this?!" || echo "Ooops, call to false failed!"
Which yields:
Code:
This prints!
So does this!
Ooops, call to false failed!
So in the above, when false failed - the echo statement after the && (AND) does not run, but the echo command after the || (OR) does run.

Typically, if there's a chance that part of your script could fail - you can use the || (OR) operator to catch that failure and log an error, or perform an alternative action instead.

Also, you can combine && (AND) and || (OR) instead of if... then else.... type syntax in your scripts.

e.g.
Something like this:
Bash:
if [[ -f /path/to/file ]] ; then
    echo "File exists!"
else
    echo "File does not exist!"
fi
Can also be written like this:
Bash:
[[ -f /path/to/file ]] && echo "File exists!" || echo "File does not exist!"
Above you have 5 lines of if..then..else condensed down to a single line, using && (AND) and || (OR). Both snippets do exactly the same thing when they are ran - they are no different.

The first version is definitely more human-readable. But the second is shorter and more concise/succinct.

Anyway - I've gone slightly off-topic. But that's the basics of using && (AND) and || (OR) to combine several commands.

The main thing to remember about them is that they are conditional! If you want to unconditionally run several commands at once in a single line, you can simply separate them with a semicolon ; instead!
 

None-yet

Member
Credits
644
Good info here. I have always been fascinated with the way software works. With someone knows nothing about it and you see people sit in front of a keyboard. It's like, all they are doing is typing stuff and then other stuff happens. You guys have been doing this for a while. But outside of the software bubble it looks as if it is magic. Like it shouldn't work but it does.

Putting commands together and creating something using a keyboard. It is like, what am I missing here? lol I look at sign language the same way. Even though my "need to know and understand" made me go learn how to sign way back when I was a kid. Very few things really transcend borders. But anywhere in the world two deaf people can communicate. Anywhere in the world a programmer can look over another programmers work and figure it out.

I am having great fun satisfying my own interest in solving small little tasks and watching them work together in unisom. Kinda makes you watch the news and wonder why we as a world can't find a way to work this way.
 

JasKinasis

Well-Known Member
Credits
2,928
And after re-reading through the thread and reading the original problem again. Rather than using && (AND) - a better way to do this might just be to pipe the output of the first command directly to sed and then let sed write your output file for you.

e.g.
Bash:
sort -u --merge --parallel=4 {1..24}.txt | sed 's/^/example    /' > 9-9-complete.txt
That way you don't have to write to an intermediate file, before passing the intermediate file through sed. The output from sort is passed directly to sed and then sed will write the file.

Also the {1..24}.txt thing should save you from having to list all 24 of the text files!
 

JasKinasis

Well-Known Member
Credits
2,928
Offhand, I don’t know. At a guess - it’s probably related to the fact that another process is writing to the file when glogg tries to open it. There could be some exceptional condition that glogg isn’t handling gracefully (or at all!) - Which causes glogg to crash. It could be any number of things. A race condition, dereferencing a null pointer, an excessively large file.
 


Members online


Top