Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 52934

Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.). Check scripts with https://shellcheck.net before posting.

12 votes

How do I run a command and get its exit code in fewer characters?

This is what the until loop is for: until some_command do sleep 1 done It is the logical negation of a while loop. The spec says: The until Loop The until loop shall continuously execute one comp …
mikeserv's user avatar
  • 59.4k
38 votes

How to check directory is empty?

if ls -A1q ./somedir/ | grep -q . then ! echo somedir is not empty else echo somedir is empty fi The above is a POSIX-compatible test - and should be very fast. ls will list all files/dirs in a …
mikeserv's user avatar
  • 59.4k
1 vote

add absolute path option to a command

opt() case ${1##*:*}:${#2} in (--:*) ! x=$((x-2)) ;; (-nonfileoption:[!0]*) p=;; (-fileoption:[!0]*) p=\$2/;; (*:0) eval $"{$x"':?Bad option::arg: "$1::$2"}';; (*) ev …
mikeserv's user avatar
  • 59.4k
1 vote

How to echo/print the last iteration in this loop

To do as asked you need only change += to =. Rather than appending to an existing value for every true case found, if you overwrite the last true case with a new one each time then when the loop ends …
mikeserv's user avatar
  • 59.4k
1 vote

Unix shell script, parameters with spaces

Another noteworthy quality about "$*" and "$@" is that they represent the shell array - the current set of the shell's arguments. These are changeable with the set built-in, but are more generally the …
mikeserv's user avatar
  • 59.4k
3 votes

How to check input parameter for running shell script

_fn() { set -- "$@" $(cat) while ${1+:} false ; do echo "$1" && [ "$1" = "arg2" ] && echo "$1" $YOUR_CHK shift done } echo "arg2" | _fn "arg1" OUTPUT arg1 arg2 arg …
mikeserv's user avatar
  • 59.4k
2 votes
Accepted

XOR of a shell variable and a binary file

Your shell can handle bitwise ops, though, for any serious processing, it's going to be awfully slow, and it can't handle anything more than say 20 or so digits at a time. Still: sh <<\CMD print …
mikeserv's user avatar
  • 59.4k
1 vote

Copying files with a specific pattern

set -- ./aaa_bbb_ccc_ddd_[!ul]* cp "$@" -t $location2 var=$*
mikeserv's user avatar
  • 59.4k
2 votes

Unix commands for generating random alphabet

rand=$(tr -dc '[:lower:]' </dev/urandom | dd bs=1 count=1 status=none) until [ "$in" = "$rand" ] && echo "correct" ; do in=$(stty raw dd bs=1 count=1 status=none </dev/tty stty sane …
mikeserv's user avatar
  • 59.4k
2 votes

Remove specific file from directory if it's the only one

find . -type f -iname "readme.txt" -exec sh -c ' for f do ls -1qap ${f%/*} | grep -v "\(readme.txt\$\|/\$\|.\{1,2\}\$\)" && echo rm "$f" ; done' \{\} +
mikeserv's user avatar
  • 59.4k
1 vote

Renaming file in scripts

f=IGNORE_NAMES_20140606-2014-06-06.txt mv "$f" "${f%%-*}.txt"
mikeserv's user avatar
  • 59.4k
10 votes

How not to print a new line with echo (and wc) in a shell script?

It would be far better if you didn't use echo at all. The use of echo when combined with arbitrary input can have unintended effects. What's more, you do not need wc to count the characters in a shell …
mikeserv's user avatar
  • 59.4k
-1 votes

Identifier must be declared

Basically you're trying to set '$trimfilename' to 'Connectivity' when you do: if '$trimfilename'='Connectivity'... if - when in command position - is not a command in the same sense that echo and e …
mikeserv's user avatar
  • 59.4k
1 vote

Press any key to pause shell script, press again to resume

With dd you can reliably read a single byte from a file. With stty you can set a min number of bytes to qualify a terminal read and a time out in tenths of a second. Combine those two and you can do w …
mikeserv's user avatar
  • 59.4k
3 votes

cd into all directories, execute command on files in that directory, and return to previous ...

cd -P . for dir in ./*/ do cd -P "$dir" ||continue printf %s\\n "$PWD" >&2 command && cd "$OLDPWD" || ! break; done || ! cd - >&2 The above command doesn't need to do any subshells - it just …
mikeserv's user avatar
  • 59.4k

1
2 3 4 5
22
15 30 50 per page