New answers tagged bash
0
votes
Change prompt colour inside midnight commander?
You cannot make the prompt colorized.
But mc has an ability to change color themes, look in Options/Appearance. There are many themes already exist and you can choose one you like the most. Or create ...
2
votes
Accepted
Bash script segmentation fault
If the "$name" it's processing is a directory, you need to call func on its contents, and not the original argument, or you get an infinite loop, and hence the segfault.
Your code can be ...
1
vote
Bash script segmentation fault
The simplest approach, if you can use bash features, is to enable globstar and use that:
shopt -s globstar
for f in "$1"/**; do
if [ -f "$f" ]; then
file -- "$f&...
0
votes
How to add a letter in front of file names in a dir when file names have digit(s) at the beginning
Even complex file renaming can be done using pipeline including while loop with read:
ls -1 |
grep '^[0-9]' |
while read f
do
mv -v "$f" "A${f}otherText.svg"
...
0
votes
Replicating floating point arithmetic WITHOUT bc utility
If you want/need to do it purely in Bash (or even Dash or Busybox), you can
take a page from How to do integer & float calculations, in bash or other languages/frameworks? and
abuse the fact that ...
3
votes
Replicating floating point arithmetic WITHOUT bc utility
If I understand your code correct, you want to log a message when the first value of /proc/loadavg is greater than CRIT_LOAD. This check including the parsing of /proc/loadavg can be done with a ...
3
votes
Remove files except latest one by numeric order
With zsh:
rm -f -- *core*(n[1,-2])
Where the n glob qualifier turns on numericglobsort for that one glob expansion and [1,-2] selects the first to second last.
Or:
rm -f core-<1-4294967295>-xyz....
-1
votes
Remove files except latest one by numeric order
Oh i found the solution.
ls -v | grep core | tail -n 1
will do the trick.
Reference:
List files sorted numerically
-1
votes
View a range of bash history
Assuming you want the range 4 to 7:
history 4 | head -n 3
In this method, you can generally get history from n to m with:
history n | head -n m-n
Note that the head command is literally head -n ...
0
votes
unix pass - passing passphrase with the usage of PASSWORD_STORE_GPG_OPTS
As an alternative to reading the password from a string, as suggested above, the passphrase can also be handled through some file:
export PASSWORD_STORE_GPG_OPTS="--pinentry=loopback --passphrase-...
2
votes
awk filter rows that only consist a single letter
awk '$NF !~ /^X+$/' filename
output
ROW 1 AA 120 APFGHKDESFNNJFHGRIHJASFGNSKDHFIXXXXXX
ROW 4 AA 89 WUAHGLIHGUNGBGDSYUXXXXXXXXXXXXXXFGOAYGIGWEIWIGFUEGFHUIWGEFU
ROW 5 AA 186 XXWANFJHOUNGRIGNO
ROW 6 AA ...
1
vote
sed or awk: replace only the n-th occurence of a string
We can do both with sed in a single command:
echo "1 aa 2 2 3 bb 5 bb 2 5"|sed -e "s/2/kop/3" -e "s/bb/pp/2"
This command will replace 3rd occurrence of 2 with kop and ...
4
votes
Accepted
How to add a letter in front of file names in a dir when file names have digit(s) at the beginning
With zsh instead of bash, to rename <digits>.svg to A<digits>Solid.svelte:
autoload -Uz zmv
To autoload the zmv batch-renaming function (best in ~/.zshrc)
zmv -v '(<->).svg' 'A${1}...
0
votes
What will this command output?
Answering the question you've asked,
for i in $(du -kx / | tr -s "\t"""| cut -d""-f2); do echo $i | rev | cut -d"/"-f1| rev; done
cut: the delimiter must be a ...
0
votes
Compare a file with stdout and print out not match line
It will most probably break your system, but you asked for it:
dpkg --get-selections | cut -f 1 | grep -v -x -f <( cat file.txt | xargs -n 1) | xargs apt -y remove
This will remove the packages ...
0
votes
Accepted
What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?
With zsh, you could do:
while IFS= read -ru3 url; do
curl -- $url
print -ru $(( $? ? 4 : 5 )) -- $url
done 3< urls 4> bad 5> good
That way, the bad and good files are opened only once ...
0
votes
Script with curl works manually but not in cron job
It's possible curl used by cron might be different from what's used in the terminal/shell. Check which curl in the terminal (where it's working), and then either directly in cron or the shell script ...
1
vote
What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?
Using bash, you can loop for the urls and test the curl command, --fail option of curl seems to be good for use inside scripts, see:
How to check whether a command such as curl completed without error
...
1
vote
iterating though files in extracted directories and running an exe against the file with parameters
Dropping the quotes around the command as per ilkkachu's comment:
for f in *; do ../nsc2e -c /netscaler/nsconmsg -K "$f" -f ../ns2ce.conf; done
... solved the issue.
1
vote
Echo with obfuscation
$ echo "helloworld" | sed -r "s/(.{3})(.{5})/\1*****/"
hel*****ld
(.{3}) - the masking starts after the 3rd letter
(.{5}) - the quantity of letters that will be replaced by *****. ...
0
votes
Is there a commonly used environment variable or configuration setting for enabling widespread application verbosity?
There is no single standard option, environment variable, or other generally accepted mechanism enabling verbose output for utilities. The POSIX standard defines no standard option for this.
You will ...
1
vote
How can I use the terminal and track what is being changed?
That requires the program to specifically log what it is doing. In the cases you have noted you get the output in the terminal when you run it from the terminal. When you run them from a GNOME ...
0
votes
Trying to run .sh script from curl
Since you're relying on a bash feature to deliver the output of curl from what the shell considers to be a filename, you need to ensure that substitution is handled by bash:
bash -c '. <(curl -s -L ...
0
votes
Accepted
cannot figure out why the file will not copy
So based on the previous comments i was giving blank values to the 'line' variable. it should have read
while read line; do cp -r ./default.meta "$line"; done < test
4
votes
sed or awk: replace only the n-th occurence of a string
To change the nth occurrence within a line, sed has syntax for that with s/pattern/replacement/3 as already shown by @HatLess.
To change the nth occurrence in the whole input, you could use perl with:
...
5
votes
Accepted
sed or awk: replace only the n-th occurence of a string
Using sed to change the second occurance of bb
$ sed 's/bb/new-bb/2' file
1 aa 2 2 3 bb 5 new-bb 2 5
or to change the third occurance of 2
$ sed 's/2/12/3' file
1 aa 2 2 3 bb 5 bb 12 5
2
votes
sed or awk: replace only the n-th occurence of a string
With awk you could do:
awk -v str='bb' -v pos=2 -v rplc='newBB' '
{
strPos=0
for(i=1; i<=NF; i++)
if($i==str && (++strPos==pos) ) { $i=rplc; break }
}1' infile
Output:
1 aa ...
0
votes
Rename files in any deep subfolder like the parent folder and move after to a central folder
POSIXly:
cd Root &&
LC_ALL=C find . \
-name '.?*' -prune -o \
-path './*/*/*' \
-prune \
-name '*.*' \
-type f \
-exec sh -c '
ret=0
for ...
1
vote
How to set `$CHROME_EXECUTABLE` in WSL2 for windows chrome
you can use the old 8.3 naming style and this works
So, the correct command would be:
export CHROME_EXECUTABLE="/mnt/c/progra~1/google/Chrome/Application/chrome.exe"
after this you can type:...
0
votes
how to compare all files inside ONE directory in bash?
Comparing many files in pairs grows cumbersome very quickly. Ten files requires 45 comparisons. 100 files takes almost 5000. My test set is 595 files (totalling 10 GB), which would require over 175,...
2
votes
awk filter rows that only consist a single letter
Using Raku (formerly known as Perl_6)
raku -ne '.put unless .words[4] ~~ /^ X+ $/ ;'
Sample Input:
ROW 1 AA 120 APFGHKDESFNNJFHGRIHJASFGNSKDHFIXXXXXX
ROW 2 AA 234 ...
2
votes
awk filter rows that only consist a single letter
Using sed
$ sed -n '/\([^ ]* \)\{4\}X\+$/!p' input_file
ROW 1 AA 120 APFGHKDESFNNJFHGRIHJASFGNSKDHFIXXXXXX
ROW 4 AA 89 WUAHGLIHGUNGBGDSYUXXXXXXXXXXXXXXFGOAYGIGWEIWIGFUEGFHUIWGEFU
ROW 5 AA 186 ...
5
votes
awk filter rows that only consist a single letter
Using standard sed to delete all lines that contain a space followed by only X at the end:
sed '/ X\{1,\}$/d' file
or,
sed '/ XX*$/d' file
With a sed that understands extended regular expressions if ...
2
votes
awk filter rows that only consist a single letter
awk -F ' X+$' 'NF == 1' file
Split line on any space, followed by just X till the end. If there are just one record, then the last field is made by only X.
perl -alne 'print if $F[-1] =~ y/X/-/ != ...
3
votes
awk filter rows that only consist a single letter
To check every field rather than only the last field, you can do:
awk -F'(^|[\t ]+)X+([\t ]+|$)' 'NF==1' infile
or explicitly check every fields using loop:
awk '{ for(i=1; i<=NF; i++) if($i ~/^X+$...
8
votes
Accepted
awk filter rows that only consist a single letter
With awk, print the lines where last field has at least one character which is not X:
awk '$NF ~ /[^X]/' file
ROW 1 AA 120 APFGHKDESFNNJFHGRIHJASFGNSKDHFIXXXXXX
ROW 4 AA 89 ...
0
votes
Bash: sed or awk rewrite of numerical sequence
GNU awk only:
echo 'v100 v201 v102 v300 v301 v500 v999 v301' |
awk -v RS='[[:space:]]' -F '' '
$0 {printf "%s", $1 (A[$0]?A[$0]:A[$0]=++i) RT}'
v1 v2 v3 v4 v5 v6 v7 v5
2
votes
Bash: sed or awk rewrite of numerical sequence
The GNU implementation of awk supports RS being defined as a regular expression and records what it matched in the RT special variable. So with it, you can do something like:
$ echo 'v100 v201 v102 ...
0
votes
Use config file for my shell script
Inspired by this article mentioned here in this response I came up with my own solution.
The idea is the same, make a filtered copy of the config file in /tmp and use this one instead of the original.
...
1
vote
Prevent sourcing a bash script, in the script itself
Based on @roaima ' s answer:
add this line early in your script.
return 0 2>/dev/null || :
It works even after set -e
It will not show an error message when not using source.
It prevents running ...
0
votes
How to sync two folders with the same file names but different extensions
in general, if you have similar filename that changes in some particular place, you can set those differences in [] brackets:
ruslanas@ruslanas test]$ ls
a.jpg a.raw a.doc
[ruslanas@ruslanas test]$ ...
5
votes
Accepted
Bash: sed or awk rewrite of numerical sequence
Using awk:
awk '{ for (i=1; i<=NF; ++i) $i = (seen[$i] ? seen[$i] : seen[$i] = "v" ++n) }; 1'
This goes through all the fields of each input line and reassigns it. The value that it is ...
5
votes
Bash: sed or awk rewrite of numerical sequence
With perl:
$ echo 'v100 v201 v102 v300 v301 v500 v999 v301' |
perl -pe 's{v\K\d+}{$seen{$&} //= ++$n}ge'
v1 v2 v3 v4 v5 v6 v7 v5
v\d+ matches on v followed by one or more decimal digits. The \...
1
vote
Bash: sed or awk rewrite of numerical sequence
If your input contains only strings of "v" followed by digits, AND you're OK with space-separated output, this perl script can do what you want:
$ echo 'v100 v201 v102 v300 v301 v500 v999 ...
2
votes
bash: determining whether a variable specifically exists in the environment?
You could spawn a new shell and inquire if the variable exists:
$ bash -c '[[ -v VAR2 ]]' && echo variable is exported || echo variable is not exported
variable is exported
$ bash -c '[[ -v ...
1
vote
how to compare all files inside ONE directory in bash?
Yes, you do need two loops. But it looks like you don't need diff because you're discarding the diff output to /dev/null, you can use cmp instead.
For example:
#!/bin/bash
# Read the list of files ...
2
votes
Accepted
GNU parset is not working in a script but works in terminal
In short: You need to do what the error message tells you to do.
Longer version: There are two things called parset. The first is a shell script that tells you how to enable the function version. ...
6
votes
Accepted
bash: determining whether a variable specifically exists in the environment?
Your title and body are very different.
Your title is either impossible or meaningless. All environment variables in bash are also shell variables, so no environment variable ever can be 'only' in the ...
1
vote
Accepted
Periodically transfer files from two remote servers to single host using shell script
Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the ...
0
votes
Accepted
How can I read each line of a text file (contains unique id) in bash using for loop, save it into a variable and use it subsequently for
When declaring a variable, you aren't allowed to have spaces around the = sign, so your file variable isn't getting set. You also need to prefix it with a $ sign when you want to iterate through it.
...
Top 50 recent answers are included
Related Tags
bash × 24838shell-script × 6171
shell × 3852
linux × 2510
scripting × 1417
awk × 891
command-line × 889
sed × 835
terminal × 710
text-processing × 647
grep × 645
ssh × 637
find × 586
variable × 576
io-redirection × 535
ubuntu × 513
pipe × 509
files × 497
quoting × 484
zsh × 478
environment-variables × 478
alias × 462
command-history × 427
wildcards × 416
array × 396