Skip to main content

New answers tagged

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. ...
Vilinkameni's user avatar
  • 1,659
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 {...
RiverHeart's user avatar
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. ...
cas's user avatar
  • 84.9k
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 ...
Stéphane Chazelas's user avatar
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 ...
waltinator's user avatar
  • 6,892
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 {$...
Stéphane Chazelas's user avatar
-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&...
Le Déchaîné's user avatar
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. ...
Chris Davies's user avatar
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....
cas's user avatar
  • 84.9k
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 ...
Seamus's user avatar
  • 3,896
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 () { ......
Arnaud Valmary's user avatar
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 ...
choroba's user avatar
  • 49.7k
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, ...
Stéphane Chazelas's user avatar
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.
waltinator's user avatar
  • 6,892
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 '' --...
Hauke Laging's user avatar
  • 94.8k
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 ...
oldfred's user avatar
  • 860
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 ...
cas's user avatar
  • 84.9k
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 ...
Betonmischer's user avatar
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 ...
Stephen Kitt's user avatar
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 ...
Marcus Müller's user avatar

Top 50 recent answers are included