New answers tagged shell-script
0
votes
LF file manager : how to go back to $OLDPWD?
Existing examples
The lf repository has several example scripts which change the working directory to the last working directory in lf on exit, one of them made for the POSIX shell: etc/lfcd.sh. ...
0
votes
How to get grep exit code but print all lines?
Fixed version of @user14755's answer which only set the exit code. This version sets the exit code but prints all lines.
echo -e "foo\nbar\nbaz" | awk '/bar/ { exit_code = 1 } { print } END {...
13
votes
Accepted
POSIX sh alternative to using [[ ... ]] in Bash
Most people use case statements for option processing because it's simple and easy and it works. There are countless examples using either built-in getopts or /usr/bin/getopt or custom/hand-crafted. ...
7
votes
POSIX sh alternative to using [[ ... ]] in Bash
expr and awk are two POSIX utilities that can do regexp matching. expr using basic regexp¹ and awk a variant of extended regexp². expr suffers from a number of design flaws and is usually considered ...
0
votes
Reading data from serial port on linux
I've used the screen utility (man screen) to explore mysterious serial devices.From man screen:
• If a tty (character special device) name (e.g. "/dev/ttya") is specified as the first ...
0
votes
Sorting output from find more like tree --dirsfirst -F
You could try something like:
find -files0-from <(printf '%s\0' "$PREFIX") -mindepth 1 -xtype d -printf '%P/\0' -o -printf '%P\0' |
perl -l -0ne '
print $_->[0] for
sort {$...
-1
votes
How do I create a directory for every file in a parent directory
Just a one-liner to copy-paste in terminal, straight into the "ParentFolder" where "File1.txt, File2.txt" are.
for x in *.txt; do mkdir "${x%.*}" && mv "$x&...
2
votes
Accepted
Replacing the domain string in the compiled binary file with an IP address
Yes, you can do this. I would use something like sed to perform the gross changes and then xxd to give me an editable file that would let me replace the remaining unwanted characters with NULs.
...
0
votes
Sorting output from find more like tree --dirsfirst -F
Not without either:
a) writing a custom sorting script to pipe find's output into, e.g. with awk or perl, and preferably using a natural sort method so that filenames with numbers are sorted correctly....
2
votes
Mount a partition from a script unless it is already mounted
First, I wonder re your use of udiskctl in a script. This udiskctl man page has the following caution:
Additionally, this program is not intended to be used by scripts or other programs...
It seems ...
0
votes
Accepted
Bash Script Linux - combines a QUIET function with other functions without hiding some of them
Use #!/usr/bin/env bash instead of #!/bin/bash;
Use only lowercase for your variable names;
Do not mix echo and printf commands;
Do not mix $... and %... in printf arguments;
function fct_name () { ......
15
votes
Is there any difference between [[ -n $1 ]] and [[ $1 ]] in bash?
There's no difference. In man bash, both the possibilities are shown as identical under CONDITIONAL EXPRESSIONS:
string
-n string
True if the length of string is non-zero.
At the prompt of the bash ...
6
votes
Accepted
Bash: only the first long option is being processed
shift 2 will remove the first 2 positional parameters so skip both --first and --second. You'd use shift 2 when processing an option that takes an argument (after having stored the value in $2). Here, ...
1
vote
Mount a partition from a script unless it is already mounted
The mountpoint command (check man mountpoint) will tell you if a directory has something mounted on it.
3
votes
Accepted
Bash: function in a script with flags
The problem is the colon which links Hello to --foo instead of treating it as a non-option argument:
getopt -o '' --long foo: -n 'testing' -- --foo "Hello"
--foo 'Hello' --
getopt -o '' --...
0
votes
Mount a partition from a script unless it is already mounted
I always create a mount point with any new install, I used to test for that & create it, but easier just to have it. Or use the defaults in /media/fred as I label all partitions.
I have never ...
2
votes
Mount a partition from a script unless it is already mounted
Your assumption/premise is wrong. You can use set -e in a script where a program might return an error as long as you catch the error and preferably deal with it (although a no-op like : works just ...
0
votes
Accepted
Parent shell script blocked from exiting by background subshell
The problem is twofold. First, I discovered that the ubus process persists through the child script's subshell termination because of the $! variable expanding to a wrong PID (corresponding to the ...
8
votes
Accepted
zsh - if condition comparison: '-ne' and '!=' have different result
-ne is an arithmetic operator, it isn’t suitable for string comparisons. In Zsh, in the [[ ... ]] construct (not the test aka [ standard builtin which follows the POSIX requirements) its operands ...
0
votes
Parent shell script blocked from exiting by background subshell
So, here's the question: under what circumstances is it possible for a background subshell to block the parent script?
if your subshell is actually launched as separate process (and I think it is ...
Top 50 recent answers are included
Related Tags
shell-script × 16836bash × 6993
shell × 2441
linux × 1944
scripting × 1188
text-processing × 1125
awk × 1007
sed × 857
files × 501
grep × 483
command-line × 422
ssh × 384
find × 377
variable × 340
ubuntu × 310
zsh × 310
cron × 287
regular-expression × 249
ksh × 234
quoting × 231
pipe × 226
io-redirection × 218
terminal × 206
date × 199
rename × 182