New answers tagged

0 votes

am not sure how to write on other file

To create a properly formatted YAML file from your variables, it is best to use a YAML-aware tool, such as yq from https://kislyuk.github.io/yq/ With it, you could write your script like so: #!/bin/...
  • 281k
0 votes

am not sure how to write on other file

If you have a variable, you can echo that variable to the file: echo "username: $name" > newfile echo "lastname: $last" >> newfile Read on "stream redirection in ...
  • 2,287
0 votes

How do script a file rename functions to remove random character suffix

In zsh: autoload -Uz zmv zmv -n '(*.txt)_[[:lower:]](#c4)' '$1' (remove the -n for dry-run if happy) Would rename the files whose name ends in .txt followed by 4 lowercase letters (replace [[:lower:]]...
2 votes

How do script a file rename functions to remove random character suffix

You can use rename command with regex for batch rename. In debian based distros, this package is called 'rename'; in arch based distros it is called perl-rename. (This regex assumes that all the files ...
0 votes

Running minicom from a bash script for X amount of time (and getting the output)

minicom documents an option to capture output: -C, --capturefile=FILE filename. Open capture file at startup. ... L Turn Capture file on off. If turned on, all output sent to the ...
  • 32.9k
0 votes

Script to change file/folder names which contain illegal characters (Synology NAS)

If bash is available on that system: LC_ALL=C find /path/to/share -depth -name '*:*' -type d -exec bash -c ' for file do base=${file##*/} mv -i "$file" "${file%/*}${base//:}&...
0 votes

Script to change file/folder names which contain illegal characters (Synology NAS)

If it's just the : that is causing trouble, you could use find with rename option to replace that character with something else. find /path/to/share -name "*:*" -exec rename 's/\:/_/' {} \; ...
0 votes

Using case and arrays together in bash

For bash users, you can use the non-short-circuiting ;;& operator; it's not case directly supporting arrays in its clauses, but it's reasonably elegant: ARR=( opt1 opt2 opt3 ); case $1 in *) ...
1 vote

Batch remove invisible characters from filenames

I'm not sure if this really is an answer as I wasn't able to make it work from macOS, but it certainly is a workaround that solved my problem. Since the external drive was a NAS, I ssh'ed into it and ...
  • 141
4 votes

Split a record by record type and report any unexpected record type

Using any awk and any sort: $ cat tst.sh #!/usr/bin/env bash awk ' BEGIN { split("E20 I47 I49 I50 Q11 R11 W55",tmp) for ( i in tmp ) { expected[tmp[i]] ...
  • 24.5k
5 votes
Accepted

Split a record by record type and report any unexpected record type

Here's one awk way. First, create a file with the "good" records, one per line: $ cat goodRecs E20 I47 I49 I50 Q11 R11 W55 Then: gawk 'FNR==NR{good[$1]; next} { rec=substr($...
  • 221k
17 votes

Why does the += operator fail to increment value?

In most Bourne-like shells (ksh, bash,zsh), the += operator works over three different variable types. For a normal variable, a string variable, this happens: $ a=hello $ a+=3 $ echo "$a" ...
4 votes

Why does the += operator fail to increment value?

In the feature-rich POSIX-like shells (ksh, mksh, Bash, zsh) the += operator is a string concatenation operator when used on regular scalar variables. The result you're seeing is exactly what you ...
  • 27.2k
-1 votes

Capabilities for a script on Linux

Now, in the year 2022 and if your system uses systemd, you may also create a systemd unit config and run the script from there, also listing the required capabilities with CapabilityBoundingSet= ...
  • 119
0 votes

Find words in a list that start with words from another list excluding vowels

With zsh: #! /bin/zsh - words=(foobar foobarbaz STRING) strings=(fbr STRN xyz) vowels=aeiouAEIOU # adjust as needed typeset -A matches=() for word ($words) { word_without_vowels=${word//[$vowels]} ...
2 votes

Need to remove files older then 90 days with an exception of last day of the month

I like the other answers, but I have a simpler solution. Also, the original question assumes that the last-of-month (date) file is always there. But we all know that you don't always have the snapshot ...
2 votes

Need to remove files older then 90 days with an exception of last day of the month

Last day of the month are the ones ending in 0131, 0331, 0430... 1231 and 0229 for bissextile years and 0228 on other years. With GNU date and a shell with support for zsh-style {x..y} brace expansion,...
3 votes

Need to remove files older then 90 days with an exception of last day of the month

Assuming that's the date at the end of the file name you want to consider (the 20220531 in ABC.txt.20220531), in zsh, you could do: #! /bin/zsh - zmodload zsh/datetime day=86400 strftime -s range '&...
1 vote

Issue - infinite loop using while on ksh script

There is no obvious reason for any loop to become "infinite". However, re-writing the exact same script but extracting each file name to a variable we get a more legible version, IMO. day=`...
0 votes

Extracting nested zip files

How can I recursively extract zip files that contain zip files in folders (nested zips in folders) & repack them into 7z format in a bash (shell) script? Or a tool with this functionality? Ex: ...
0 votes

Automate user input into script (using ssh)

Either use expect or try this ugly hackery ( sleep 2; echo 'cd /map1'; sleep 2; echo 'reset'; sleep 2 ) | ssh ipmiService
  • 93.6k
1 vote

Read and convert date from file

$ let diff=($(date +%s -d 'today')-$(date +%s -f VM1_EOMAP_TIME.log))/60 $ echo "$diff"
  • 2,119
1 vote

Read and convert date from file

Your problem is quoting. And that you should use date -u to compensate for timezone. $ echo "07 Sep 2022 16:30" > infile $ var="$(<infile)" $ cat infile; echo "$var"...
1 vote

Read and convert date from file

If your bash is recent, the current time in epoch seconds is builtin: $EPOCHSECONDS Otherwise, bash's printf can get it with the %(fmt)T directive and magic value "-1": printf -v ...
0 votes

Shell Script - Check whether a single character input is uppercase or lowercase or special character

be sure to wrap the variables in quotes - if you are testing a regular expression as a string, for example, you could enter the following: echo "enter a US state name" read var_1 if [[ &...
3 votes
Accepted

How can hostnames vary without having to add another name?

/etc/hosts only maps fixed IPv4/v6 addresses to fixed names, and vice versa. There are no wildcards available in its syntax. If the rather simplistic /etc/hosts is inadequate for your requirements, ...
  • 72.4k
0 votes

Retrieve X11 window ID of a just-launched GUI-program

Using python gi library, (assuming that this program that has been launched most recently is on top of the window stack) import gi gi.require_version('Wnck', '3.0') from gi.repository import Wnck , ...
0 votes

How to send variable to an inline shell-script?

How about a subshell instead? When I used to use inline scripts often it was because I wasn't aware of how to use subshells. I just found the answer for how to pass variables to a subshell, but this ...
  • 435
1 vote

Linux: Autorun : "Run file from USB when usb is inserted "

By default scripts called autorun.sh on any drives root directory will be automatically executed at least on Ubuntu Desktop. However be aware that you need to be able to set execution permissions on ...
0 votes

Is there a way to activate a particular tab of chrome via bash?

I tweaked Geoffroy's answer a bit since it didnt work for me. wmctrl -lx is used to get all windows and their titles. The current tab title is inside the windows tab name. wmctrl -ia is used to ...
1 vote
Accepted

How to create a sh script that can give as output the exact string I want using the awk command

This code may work (stripping first two symbols from the string): ps ax | awk '/bhyve: [vm]/{print substr($6,3)}'
  • 14.7k
2 votes

regexp to only extract first set/group of numbers from line of string from a file

Given the file shell> cat /tmp/login.defs PASS_MAX_DAYS 30 PASS_MAX_DAYS 30 .12 PASS_MAX_DAYS $30 PASS_MAX_DAYS 30 23 PASS_MAX_DAYS 30 days PASS_MAX_@DAYS 30 Read the file - command: cat /tmp/...
0 votes
Accepted

How to extract txt files from multple folders and merging all into a single folder?

Go to the root folder of 100 sub folders and find all of the .txt files, copy them into the new folder ~/tests: find ./ -type f -name "*.txt" -exec cp "{}" ~/tests/ \;
  • 303
1 vote

extract TOC from epub

The way your script is written, the for loop only has one thing to iterate over, a single filename (which probably doesn't exist) called "1.epub 2.epub". That's not a list of two filenames, ...
  • 69.9k

Top 50 recent answers are included