The control-flow tag has no wiki summary.
4
votes
3answers
78 views
Bash: handling “[[ statement ]] || echo problem found ; exit 1” logic
In the statement in the title, there is a problem that even if the statement is true, it will still exit with status 1 because, as I understand it:
[[ statement ]] || echo problem found ; exit 1
...
3
votes
1answer
40 views
Bash to print out the make result
To compile my project and to check if it's good or not, I want to print out the status message after make command. Which one should I use?
make && echo “Success” || echo “Fail”
make || echo ...
5
votes
5answers
115 views
shell script to control the number of processes of an user
I have to launch a total number, NUMTOT of a program (could be any, C, Mathematica, ns-2... mathematical environments).
The problem is that the computer where these processes run only admits a ...
1
vote
2answers
93 views
Shell: Break both if and for loop at once
Shell: Break both if and for loop at once.
My script: ip to name resolve code
IP=192.168.27.191
hostNameChecker()
{
if [ `getent hosts $1 | wc -l` -ne 0 ];then
HOST_NAME=`hostname`
...
4
votes
3answers
488 views
Loop through the lines of two files in parallel
The object of the script I'm making is to compare two series of files. The file names are themselves stored into two separate files, one path per line. My idea is to have two while read loops, one for ...
7
votes
2answers
144 views
tmux not respecting disabled control flow
In my shell I have flow control disabled using stty -ixon. This works
perfectly in the shell and when I launch tmux and start programs within
tmux.
However, when starting a new session from the ...
2
votes
4answers
256 views
bash while loop that breaks at a given file size
So, I bought this book called Primes and Programming, and it's pretty tough going. Today I wrote this (simple) program from chapter 1:
#!/usr/bin/env python
import math
def find_gcd(a,b):
while ...
5
votes
2answers
109 views
[ vs [[ : which one to use in bash scripts? [duplicate]
The zsh man page, in its section on test (aka [), explicitly advises against using it at all, and urges readers to use [[ whenever possible.
The relevant section states:
The command attempts to ...
3
votes
1answer
2k views
do…while or do…until in POSIX shell script
There is the well known while condition; do ...; done loop, but is there a do... while style loop that guarantees at least one execution of the block?
4
votes
2answers
392 views
Requesting user input while reading file line by line
For class I need to write a Bash script that will take the output from ispell and when I try and request user input inside the while loop it just saves the next line of the file as the user input.
...
2
votes
1answer
747 views
In a Bash Script how does the continue command work with embedded loops?
I am writing a bash script in a busybox session.
The script has to initiate an external executable numerous times in sequence in daemonised form then monitor the output.
while read LINE; do
...
4
votes
1answer
3k views
shell backup script, missing `]`
I'm putting together a simple backup script that will tar contents of a folder, then move that file to a backup server. The script makes sure that the tar file exists and is not zero bytes before ...
2
votes
3answers
1k views
Can I do ssh and do for loop and touch files in a single shot?
for account in ${accounts}
do
`ssh -q id@server "touch EVENTS_${account}_${date}.log"`
done
Instead of doing ssh so many times, can I just do ssh and run the for loop and touch the files?
4
votes
3answers
281 views
How to pipe to multiple places with vertical bar?
I have a folder full of files with a .dot extension like this:
a.dot
b.dot
c.dot
I want to go through all the files in this folder and run a command on them such as this:
dot -Tpdf a.dot -o a.pdf
...
2
votes
1answer
484 views
Keep global variables values piping through functions
I wrote a little bash script using sed on some html pages to extract some urls.
To avoid each time grabbing sed results in a variable then read it again I simply made 3 functions and piped together.
...
4
votes
2answers
354 views
Why does this shell snippet to check if hosts are up using netcat stop prematurely?
Q: Why does the second iteration exits after 10.175.192.16? Can someone explain that? Or I just found a "while/netcat" bug?
a.txt's content:
$ cat a.txt
10.175.192.14
10.175.192.16
10.175.192.17
$
...
3
votes
3answers
318 views
Multiple Command List After Shell Conditionals, &&, ||. Shell Script, Dash
I'm trying to do multiple commands after a condition, so for example...
[ $VAR ] || echo "Error msg" ; echo "exit"
and the Inverse
[ -z $VAR ] && echo "Error msg" ; echo "exit"
I know ...
1
vote
2answers
174 views
conditional expression and expansion
I don't seem to understand how to "test" the conditional operators [[ and [. I tried using various form such as
echo [[a=a]]
[[a=a]]
echo $?
and some other things
I want to see what they ...
7
votes
4answers
5k views
How do I reverse a for loop?
How do I properly do a for loop in reverse order?
for f in /var/logs/foo*.log; do
bar "$f"
done
I need a solution that doesn't break for funky characters in the file names.
6
votes
2answers
195 views
Send task to background in an “if”
Why is this?
if true; then sleep 3 &; fi
bash: syntax error near unexpected token `;'
I want to run
sleep 3
in the background so that the command ["sleep 3" is just an example] would run ...
7
votes
2answers
339 views
Why does 'for anytext; do' loop four times?
Is this a bug or a feature?
It seems to happen for unquoted alphanumerics.
Running GNU bash, version 4.1.5
for alpha01234; do echo $((++i)); done
output:
1
2
3
4
9
votes
2answers
2k views
Confusing use of && and || operators
I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. ...
5
votes
4answers
4k views
How to conditionally do something if a command succeeded or failed
How can I do something like this in bash?
if "`command` returns any error";
then
echo "Returned an error"
else
echo "Proceed..."
fi
3
votes
1answer
412 views
Low-level serial configuration?
I'm using embedded Linux platform, kernel is 2.6.
I'm using stty command to configure ttyS* high level.
The problem is that when I want to turn hardware flow control on:
stty -F /dev/ttySA0 crtscts
...
4
votes
2answers
132 views
Why do newlines mess up my while condition?
I have a while loop that allows setting variables when written one way but does not allow it when written another way. Why is that? This prints var as 1234 and does not print var2
#!/bin/bash
...
1
vote
2answers
876 views
Displaying and updating a counter in bash
I think it's something like this: (Fedora14/bash)
#!/bin/bash
for i in {0..10..1}; do echo -e "$i"'\c'
echo -e "\n\r"
sleep 1
done
But it doesn't work.
Purpose: like this, but without the ...
-1
votes
1answer
551 views
Loop examples in bash [closed]
Any examples for "loop" in bash?
for i in {1..5}; do echo $i; done
1
2
3
4
5
AND
for i in 1 2 3 4 5; do echo "Welcome $i times"; done
Welcome 1 times
Welcome 2 times
Welcome 3 times
Welcome 4 ...
1
vote
3answers
724 views
Ls with spaces + variables
I want to do something like this, but it doesn't save the variable after the piping ends:
fs=( )
echo ${fs[@]}
ls -A1 |
while read f
do
echo ${fs[@]}
fs+=( "$f" )
echo ${fs[@]}
done
echo ...
-1
votes
3answers
346 views
calling functions within a function
I need to fetch the results of different time ranges and email the results, but i am having trouble with the email part of the script,
how can i send an email with both results? (15min range and ...
6
votes
1answer
826 views
Most efficient way to run 2 while loops
Currently I use two different while loops to start my window manager, dwm, and the status bar that prints system info to it.
My solution at the moment is to run them consecutively in the same script, ...
4
votes
2answers
4k views
loop curl get request bash
I'm trying to automate a curl script and eventually make it loop also. I found that I can't use any loop like:
for i in `cat authors`
do
curl *line here* ...
15
votes
1answer
7k views
How to loop over the lines of a file?
Say I have this file:
hello
world
hello world
This program
#!/bin/bash
for i in $(cat $1); do
echo "tester: $i"
done
outputs
tester: hello
tester: world
tester: hello
tester: world
I'd ...