New answers tagged

1 vote

Is there a tool for collapsing output of a command base on regex?

Very basic solution in TXR Lisp: $ program | txr stcoll.tl Where stcoll.tl is: (let ((states (hash))) (whilet ((line (get-line))) (when-match `State @s: @status` line (set [states s] ...
Kaz's user avatar
  • 7,781
0 votes
Accepted

Access values of associative array whose name is passed as argument inside bash function

You need to make the arr variable a nameref. From man bash: A variable can be assigned the nameref attribute using the -n option to the declare or local builtin commands (see the descriptions ...
terdon's user avatar
  • 235k
1 vote
Accepted

"mv" command with asterisk character ("*") executed using "ssh"

The translation into another language of that: ssh admin@$host mv repo/environments/qa/* /repo shell command (assuming a POSIX shell such as sh or bash), would look like: execute("ssh", ...
Stéphane Chazelas's user avatar
0 votes

How can I make this script run in the background?

What's wrong with a cron job ?
Vorac's user avatar
  • 2,967
0 votes

Store part of file name as variable in Linux Shell

You already have code that iterates $file across your set of files: name=${file%.*} # Strip extension name=${name##*/} # Strip leading path If you insert a debugging line ...
roaima's user avatar
  • 108k
0 votes

Store part of file name as variable in Linux Shell

Yes, easily, like this: #!/bin/bash # test wrapper file="017251004_2301941_5193716.xml" num=$(basename "$file" ".xml" | cut -d_ -f3) printf "file=$file,num=$num\n&...
waltinator's user avatar
  • 4,483
-2 votes
Accepted

Append not working well in bash

I just tried using a CSV file instead of a TXT file for the discarded elements. Now the script works well. for file in folder/* do value=$(cat "$file" | awk -F, '{print $2}') ...
BelBillo007's user avatar
0 votes

How to run scripts from Awesome Window Manager

Maybe I'm too late to answer your question but I hope this helps. I too run a script for automating wifi management using iwd (iwctl) I named the script wifi and put it in the bin directory. As for rc....
Blackie's user avatar
0 votes

How to check the headers between 2 CSV files in unix?

This might be what you're trying to do, untested and using an awk that supports nextfile, e.g. GNU awk: awk ' BEGIN { FS="," } FILENAME == ARGV[1] { nf = split($0,a) ...
Ed Morton's user avatar
  • 29.2k
2 votes
Accepted

How can I pass $@ to exec.args of Maven?

When the shell expands -Dexec.args="$@" and the list of positional parameters is arg1, arg2, arg3, then you get the list -Dexec.args=arg1, arg2, arg3 (three separate arguments for mvn). The &...
Kusalananda's user avatar
  • 323k
0 votes

How do I tell KDE Konsole to open with a specified geometry and window title?

I found that konsole --help-all gave some additional QT options, one of which was --qwindowgeometry which does work. However, the size params are pixel based, which is a bit less convenient. It did ...
Gringo Suave's user avatar
2 votes

Append not working well in bash

There are two main issues here: Never do for file in $(ls). This is also known as bash pitfall number 1. First, it is fragile and, as you have seen, breaks on even slightly strange file names. What's ...
terdon's user avatar
  • 235k
0 votes

How to check the headers between 2 CSV files in unix?

The simplest thing to do is just compare the first lines of each file and move to the right directory depending on if they are identical: $ diff -qs <(head -n1 file1.csv) <(head -n1 file2.csv) ...
terdon's user avatar
  • 235k
2 votes

Replace last nonblank line with a command output

You could reverse the file and replace the first non-blank line printf -v new_line 'CONFIG=%s' "$(jq -c . config.json)" tac local.env \ | awk -v line="$new_line" ' !n &&...
glenn jackman's user avatar
1 vote
Accepted

Replace last nonblank line with a command output

I don't know about better (the best would be to know that it's the line that sets PARAM2 that needs replacing, but you specifically say "the last non-empty line"), but the ed editor is ...
Kusalananda's user avatar
  • 323k
-1 votes

Expansion of "$@" inside heredoc

Here is my attempt to create a POSIX-friendly solution: #!/bin/sh quote () { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/" ; } for x in "$@"; do x=$(quote ...
Watcom's user avatar
  • 99
1 vote
Accepted

Bash script variable syntax: with some commands it works, with others it does not

Don't put quotes around tilde ~ if you expect the shell to expand it to your home, so: SSH_CONFIG_FILE=~/.ssh/config if you have space(s); do var=~/'foo bar/file' With the quotes on tilde, you ...
Gilles Quénot's user avatar
0 votes

Create tree of processes

I'd suggest taking a snapshot of the processes currently running on the system. The absolute minimum that you'd need is each PID and its corresponding PPID: ps -ax -o pid=,ppid= This will give you ...
Kusalananda's user avatar
  • 323k
0 votes

JSON correct construction

Given a stream of tab-delimited key and value pairs, construct a single JSON object containing those keys and values. (If the data contains actual tabs, simply use a character that is not part of the ...
Kusalananda's user avatar
  • 323k
-1 votes

Mysql output format in Bash Script

mysql -N -B moodle -e "select id from mdl_course;" output abcd@pqrs:~/backup/script $ mysql -N -B moodle -e "select id from mdl_course;" 1 2 4 5 8 9 10 11 12 14 16 17 3 6 7 15
ShaileshKumarMPatel's user avatar
0 votes

Find biggest files or directories

I use this command for the root folder / to check where the biggest directories are located, but you can use in your case /tmp or any other directory sudo du / -hxd 1 -t 1M | sort -h -h from human-...
João Pimentel Ferreira's user avatar
0 votes

Best way to make variables local in a source'd bash script?

A variation on wrapping your whole script in a function is to let the sourced script source itself through a function: Let bleh.sh be the source-ing script #!/bin/bash echo -e "\x1b[34mI am $(...
BigSmoke's user avatar
  • 101
0 votes

Write values with embedded commas from arrays into CSV file using BASH

This answer assumes that your data contains no literal tabs. Start by creating a simple column for each array (with header), and combine them using paste, creating a TSV data stream: #!/bin/bash arr1=...
Kusalananda's user avatar
  • 323k
1 vote

Hi, I am trying to find all the words in a text that appear only once in UNIX. What can i do differently?

With awk: cat *txt | tr '[:upper:]' '[:lower:]' | tr -s '[:punct:][:space:]' | awk '{a[$1]++}END{for (i in a) if (a[i] == 1) {print i}}' In your example, you can't do cat files | ... | ...
Gilles Quénot's user avatar
1 vote

How to call script with timestamps relative to now, on Solaris?

Since version 11, Solaris' /bin/sh is actually ksh93 whose printf builtin can do advanced date manipulation and formatting (a lot more advanced than GNU date as a matter of fact). I would still use #! ...
Stéphane Chazelas's user avatar
4 votes

How can I make this script run in the background?

Several suggestions to run it in the background or as a conventional daemon, but this needs to run inside the session of the active user (since it wants to access the DISPLAY of the authenticated user)...
symcbean's user avatar
  • 5,110
0 votes

Is there a way to prevent sed from interpreting the replacement string?

If you need to do this in a script, you can go with an escape function, i.e.: #!/bin/bash escvar () { sed -e 's/[\/&]/\\&/g' <<< $1 } replacement='https://google.com/?query=some\...
onestep.ua's user avatar
-1 votes

How to do nothing forever in an elegant way?

When reading these answers you have to ask yourself: Is the CPU idling while your no-op command executes? You don't want a solution which burns CPU cycles while doing your no-op command. Most answers ...
peterh's user avatar
  • 922
8 votes

How can I make this script run in the background?

Create a systemd timer unit to have it run at regular intervals in the background. /etc/systemd/system/my-batterywatcher.service [Unit] Description=my battery watcher [Service] Type=oneshot ExecStart=...
Gerald Schneider's user avatar
2 votes

How to call script with timestamps relative to now, on Solaris?

It is possible (with a little lateral thinking) to do this on a standard Solaris system (without GNU date). Set up a cron job to run the script every two hours. On entry, the script runs date and ...
Paul_Pedant's user avatar
  • 8,258
3 votes

How to call script with timestamps relative to now, on Solaris?

Your script takes four arguments on its command line that correspond to a start date and time, and an end date and time, and you appear to want to call your script without specifying specific ...
Kusalananda's user avatar
  • 323k
0 votes

How to extend piped command by contents of variable?

You can pipe to awk for instance to prefix each line with the file name: "$mplayer" "${playlist[@]}" "$url" "${mplayerflags[@]}" \ -msglevel all=2 -dumpstream ...
Stéphane Chazelas's user avatar
0 votes

How to extend piped command by contents of variable?

You can't achieve that by changing the line; just print the file name without a newline before you call mplayer.
Marcus Müller's user avatar
1 vote

Shell command- to extract a string from xml file

Don't use sed nor regex to parse HTML/XML you cannot, must not parse any structured text like XML/HTML with tools designed to process raw text lines. If you need to process XML/HTML, use an XML/HTML ...
Gilles Quénot's user avatar
4 votes

How can I make this script run in the background?

As already said, you can simply use & to run in background. If you need to detach/attach, the way to go is to use tmux or screen, that way, you can attach/detach a session as you need. To run a ...
Gilles Quénot's user avatar
8 votes
Accepted

How can I make this script run in the background?

You have 2 ways to do this: Add an ampersand after the command ./battery-script.sh & This will keep the command running even after you close the terminal, by spawning a subprocess within your ...
juliethefoxcoon's user avatar
3 votes

Shell command- to extract a string from xml file

Use an XML parser to parse XML data. xmlstarlet is one. Given file.xml containing <root> <version value="GPK5" revision="B" type_of_change="1"/> <version ...
glenn jackman's user avatar
0 votes

Can't indent heredoc to match code block's indentation

There have been quite many good answers to this question already. However the one thing that I would like to improve is that most answers either require the use of tabs, or they remove an arbitrary ...
emmenlau's user avatar
  • 101
1 vote

How do I print out the file size in a shell script?

It seems, you want to try a command substitution. In your example you have var fileName=`cat '"#{IPA_FILE_NAME_FILE}"'` as an valid command substitution. Your second command substitution ...
John Goofy's user avatar
2 votes
Accepted

Run wget command on each line of a file and download the files (two at a time)

You can put multiple read commands in the while loop. For "two at a time", but them both in the background and then wait for them to complete while read -r first && read -r second; ...
0 votes

Is there a way to convert a zip to a tar without extracting it to the filesystem?

With libarchive's bsdtar: bsdtar -zcf file.tar.gz @file.zip From the manual: tar creates and manipulates streaming archive files. This implementation can extract from tar, pax, cpio, zip, jar, ar, ...
Stéphane Chazelas's user avatar
0 votes

How can I check for the existence of wikldcard strings in a list of multiple strings?

Late reply, however this command command might help: grep -E ^(develop|feat\/.*)\\\\EL$ You may need to add more options, such as -q or --quiet if you do not want grep to write errors and/or matching ...
qqqq's user avatar
  • 21
6 votes

cmp doesn't behave as expected

echo "$timestamp - Hash has changed! Sending notification. Writing new value to hash1" >> "$log" cp -f $sdir/hash1 $sdir/hash2 You say you are writing a new value to hash1, ...
telcoM's user avatar
  • 88.4k
2 votes
Accepted

Using find to delete in multiple directories with one command

You need to let your IDE know which shell is going to be used to run your code. The best way to do this is to add the appropriate shebang: #!/bin/bash BASE_PATH=/data/jobs/runs env=test find "${...
Stephen Kitt's user avatar
12 votes
Accepted

why [] test makes this script to fail?

I also understand set -e makes the script to exit on any error. Not "on any error". There are exceptions, one of them is: The shell does not exit if the command that fails is […] part of ...
Kamil Maciorowski's user avatar
2 votes

How do I print out the file size in a shell script?

The bash idiom for catching the output from commands like: du -h fileName.ipa | awk '{print $1}' and saving the output into a variable is: fileSizeInfo=$(du -h fileName.ipa | awk '{print $1}') This ...
Sotto Voce's user avatar
  • 3,923
0 votes

Using a sed command to trim extra spaces,characters, and decimals stuck

Ignoring the discussion about the whole input thing: If sed 's/[^0-9.]//g' works for you except for the trailing dots, I suggest to simply remove trailing dots: sed 's/[^0-9.]//g;s/\.*$//'
Philippos's user avatar
  • 13.3k
1 vote

How to do wildcard/glob expansion when the pattern and the resolved pathes contain spaces?

Thanks to the comment by @ilkkatchu, I now understand that I simply have to use something else than echo, so I came up with a simple inline bash script that prints every received argument as one line ...
Forivin's user avatar
  • 817
4 votes

How to do wildcard/glob expansion when the pattern and the resolved pathes contain spaces?

It seems like basic shell expansion just resolves the paths and separates them by space, It's not that stupid, and that couldn't even work. The key here is that when the command line is processed, it'...
ilkkachu's user avatar
  • 134k
3 votes

How to do wildcard/glob expansion when the pattern and the resolved pathes contain spaces?

When the globbing operators like * and ? are quoted, their special meaning is disabled. However, you need quoting or escaping to protect spaces. The solution is to quote or escape only the parts of ...
Kaz's user avatar
  • 7,781

Top 50 recent answers are included