The control-flow tag has no wiki summary.
5
votes
2answers
47 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
128 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
86 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
270 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?
3
votes
2answers
297 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.
...
1
vote
1answer
321 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
2k 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
817 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
226 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
255 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
211 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
254 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
135 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 ...
6
votes
3answers
3k 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
151 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
326 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
922 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. ...
3
votes
3answers
2k 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
319 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
119 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
477 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
266 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
465 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
2answers
255 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 ...
5
votes
1answer
618 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, ...
3
votes
2answers
3k 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* ...
12
votes
1answer
3k 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 ...