All Questions
Tagged with bash-script or shell-script
16,787 questions
1
vote
1
answer
121
views
Is [..] posix compliant while [[..]] is not [duplicate]
I read somewhere that in a shell script the [..] construct like for example [ -e $HOME/temp ] is POSIX compliant. While the script [[..]] construct like [[ -e $HOME/temp ]] is not.
Is this true?
Apart ...
0
votes
1
answer
1k
views
Bash script to copy and keep x4 backup files (daily, weekly, monthly, six-monthly) in second directory
I have an application which is making automatic daily backups at 11pm in a directory dirA. Each backup is a single .tar file, and has an apparently random naming convention that I don't have control ...
0
votes
3
answers
146
views
how to process a sequence of items in groups of N at a time
Sequentially one after another:
for i in $(ls -1) ; do command $i ; done
All at one time:
for i in $(ls -1); do \
( \
command $i ; done ; \
) & \
done; wait
How to say only for example 4 or ...
2
votes
1
answer
905
views
How to unset environment variables using a bash script
I tried to use a bash script to add or remove proxies in bulk, but the script runs with confusing results.
I can add proxies in bulk using the following code. This code is saved by me in use_proxy.sh:
...
3
votes
2
answers
702
views
Linux wait command returning for finished jobs
I came across some weird behavior when using the wait command for running parallel jobs in a bash script. For the sake of simplicity I have reduced the problem to the following bash script:
#!/bin/...
3
votes
1
answer
455
views
bash scripting: Saving and then processing file contents as a bash variable
I have a super complicated bash script that extracts results from a large output file (produced on a LINUX machine, just in case this is relevant). As part of this process, I use combinations of grep, ...
6
votes
3
answers
1k
views
How can I assign a heredoc to a variable in a way that's portable across Unix and Mac?
This code works fine on Linux but not Mac OS:
#!/usr/bin/env bash
foo=$(cat <<EOF
"\[^"\]+"
EOF
)
printf "%s" "$foo"
It fails on Mac with
./test.sh: line 6: ...
0
votes
1
answer
63
views
How can I add jpg file name in top left corner of each pdf file created with img2pdf?
find . -name 'Vol_*.tif' -print0 | sort -z -V | xargs -0r img2pdf -o out.pdf
CAS wrote above and with this I was able to solve my needs.
One extra feature: For each TIF file I would like to print on ...
0
votes
1
answer
235
views
Difference between -v, -z and -n while comparing variables in a shell script
There are the following ways to check if a variable is available or not in a linux/unix shell script
[ ! -z "${MYVARIABLE}" ]
OR
[ -n "${MYVARIABLE}" ]
OR
[[ ! -v MYVARIABLE ]]
...
2
votes
2
answers
496
views
How to prevent a bash script from running repeatedly at the start of the terminal
I am physically connected to the machine an I use Arch linux. I use Arabic in the terminal and I use BiCon to add bidirectional support for the terminal (I am using st).
I want the program to start ...
1
vote
0
answers
39
views
Why first _ is not counted as positional argument? [duplicate]
Lets take a look at my example
bash -c 'echo $#' _ x
gives
1
bash -c 'echo $#' _ x _ y
gives
3
why?
1
vote
5
answers
468
views
Convert Date Format %Y%m%d%H%M%S to unix epoch format
I am trying all the methods but I get this error:
I want to convert this format to epoch -->
date "+%Y%m%d%H%M%S"
example --> 20240913235959
I also tried other formats such as:
date &...
1
vote
3
answers
78
views
Extract information from dot separated hierarchical list
I am trying to extract a value (nread) from sysctl, but only for a specific dataset_name. What is the most efficient way to do this with a shell script?
Example sysctl output (via "sysctl kstat....
1
vote
2
answers
277
views
How to remove empty csv lines of csv files in batch
I'm using git bash, so I suppose it's ok to ask questions in Unix forum, but I'm not familiar with Unix, only use ls, find and grep, so pls correct me if I'm wrong.
I have hundreds of .csv files and ...
4
votes
2
answers
708
views
I want to write a script that simultaneously renders what's on my webcam to a window on my screen and records a video
I have tried a combination of:
v4l2loopback
ffmpeg
mpv
But either the web-cam view, or the web-cam record works, but never both together.
Can anyone provide a script that achieves this?
This is what ...
2
votes
2
answers
799
views
using gnu date to set a time and format and subtract
Can I give GNU date a date and format string for that date and tell it to also subtract some time from it? It seems I can do 2 of 3 in 1 command but not all
I have this date format:
09/Sep/24 05:02 AM ...
0
votes
0
answers
60
views
Script to copy quotes from Zotero to Obsidian
I'm struggling to make this script run correctly more consistently.
I wrote this Bash script that copies a selected, highlighted quote in Zotero (a source manager), and copies that quote into Obsidian ...
0
votes
0
answers
90
views
Pass in argument of directory for find command to ignore in script
When I call the script (via commandline) I'd like to pass in an optional list of directories for the find command to ignore. This is what I have but my output includes data from files in the ...
3
votes
1
answer
642
views
Strange variable scope behavior when calling function recursivly
EDIT: sorry. this is my first question here.
Here is a minimal working example
#!/bin/bash
#
count=0
function something() {
if test -f "$1"; then
# is a file
((count++))
...
1
vote
6
answers
899
views
Bash script that takes multiple path arguments and checks if files can be successfully created there
I would like a bash shell script (for my Debian 12 system) which accepts multiple paths as arguments, and then checks if a file can be successfully created in each one. Then the result should be put ...
0
votes
2
answers
2k
views
Dash gives error "sh: 1: cannot create : Directory nonexistent"
I am running the following script in Ubuntu 22.04:
#!/bin/bash
logfile="/absolute/path/to/log.txt"
find /absolute/path/to/haystack -type d -name "needle" -execdir sh -c 'pwd > $...
0
votes
2
answers
126
views
Script to multiply values in files
I have multiple of text files containing values like this:
width = 64
height = 128
position_x = 48
position_y = 48
I would like to go over this files, and multiply those values by 2, producing a file ...
1
vote
1
answer
264
views
bash: script running in pm2 unable to access file descriptors files at /dev/fd/
I have script script.sh:
#!/usr/bin/env bash
# pm2 seems to always run in `bash` regardless of `#!`
echo "running in $(readlink -f /proc/$$/exe)"
# Redirect to both stdout(1) and stderr(2)
...
6
votes
3
answers
1k
views
bash script quoting frustration
This problem is driving me crazy. From the command prompt I can enter this command and it works as expected (records where the INFO/RegionType tag contains the value Core are emitted in the output ...
0
votes
1
answer
139
views
Cut command failing
We have a ksh Script that is suppose to give the output as last day of the month but when ever we run it we get the below error:
cut: fields are numbered from 1
Try ‘cut —help’ for more information,
...
0
votes
1
answer
103
views
Check if a directory was deleted and later recreated with the same name
Is it possible to reliably check with a shell-script if a directory was deleted and later recreated with the same name in Linux? Depending on the filesystem one could check for a change in the inode-...
2
votes
2
answers
226
views
putting each character of multiple lines into a register corresponding to its relative position in the line
Awkard way to pharse it in the title ik. I'll break it down better so that it makes sense. I have a file that says:
abab
baaa
babb
I need to read each character of a line into a register ...
3
votes
2
answers
322
views
shell script to match a function in a file and replace it with another function in another file
I have files that contains multiple functions like below.
file1.c:
static void function1()
{
code
}
...
static void functionX()
{
code
}
...
static void lastFunction()
{
code
}
file2.c:
...
0
votes
1
answer
359
views
How to determine from the lspci output how cards and how many ports are on a card?
I need to write a script to count how many 2 or 4 port HBA cards we have in each of our servers.
I'm running lspci on each of the servers, however, none of the documents I've read so far is telling me ...
0
votes
2
answers
99
views
Preview locales
How do I quickly preview what a locale looks like? E.g: is there something like
$ localepreview en_DK.UTF-8
Of course, I immediately think of a script like
#!/bin/bash
LOCALE=$1
export LANG=$LOCALE
...
0
votes
1
answer
229
views
How does "Useless cat. ... or 'cmd file | ..' instead." should be applied?
I have the following line:
isUbuntu=$(cat /etc/os-release | grep '^ID=ubuntu$' | wc -l)
It works fine
Now, through VSC and mostly with the ShellCheck extension is indicated two warnings messages as ...
-4
votes
1
answer
119
views
Moving file with date using Unix command
I want to move all .XML files in the current working directory whose name starts with DAPRETAIL followed by the current date in YYYYMMDD format to a separate directory.
I tried:
DATE='date +%Y%m%d'
mv ...
2
votes
2
answers
386
views
commands execution based on file size fails with no apparent issues
I was working on an array job for a small pipeline, and I happened to need a way to execute a specific command based on file size. I found this post and similar which describe how to do it. At the ...
0
votes
3
answers
203
views
My shell script exits if I try to mount from within the script an already mounted drive How can I make my shell script resume?
This is what happens.
I manually and successfully mount my USB drive using mount /media/usb from outside a shell script which I wrote. Therefore, the drive is working and mounted normally.
The very ...
0
votes
0
answers
719
views
Running apt-get update in Debian Docker container hangs on installing bookworm
This is probably the strangest error I've run into.
So I'm running a Wikibase Docker setup on a Windows machine. The Docker containers are all Debian OS.
In order to install some packages upon setting ...
0
votes
2
answers
162
views
"Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that as a temporary file name" [closed]
man mktemp:
The mktemp utility is provided to allow shell scripts to safely use temporary files. Traditionally, many shell scripts take the name of the program with the pid as a suffix and use that ...
0
votes
1
answer
2k
views
bash 5.2.x + ternary operator: How can I use this combo when checking if my string variable is empty or not?
I have tried
#!/bin/bash
distro=""
myvar1=[[ -n "$distro" ]] && echo $distro || echo "debian"
myvar2=$((-n $distro ? $distro : "debian"))
these are ...
1
vote
3
answers
207
views
why my shell script generates zombies and sometimes works weird
I have a simple script for the i3 window manager that shows an i3bar at the bottom of the screen when you touch its edge with the mouse. It almost always works fine. However, after it quits, there is ...
1
vote
1
answer
163
views
How to run a command that requires input/output location on all folders in a directory, while correspondingly naming the new output folders?
For context, I have some experience with Unix and basic commands, but I'm seriously struggling to conceptualize more complex manipulations of files. Essentially, I have this command I want to run from ...
0
votes
0
answers
125
views
Automating the moving of files
I'm running a simulation for a project.
Each simulation run generates many .dat files.
So, essentially, I'm getting Z_image_000.dat to Z_image_276.dat.
I'm running multiple simulations in a day, so ...
0
votes
1
answer
108
views
Evaluating exit code of pipe or fifo
So i have something along the lines of:
set -eo pipefail
ssh localhost "ls; exit 1" | cat > output
I thought pipefail would prevent cat to write to ouput (because of the exit code from ...
0
votes
0
answers
57
views
Complex logical expression (wrong or as intended?) [duplicate]
I have a 3 part logic (A or B and C). Logically if A is true then true, or if B and C is true then true; But here is the error
if (A or B and C) -> should return true
if [ 1 -eq 1 ] || [ 2 -eq 2 ] ...
1
vote
0
answers
189
views
exit from running, sourcing, or pasting a series of commands in bash
I'm writing a script that I run directly, "source" from the bash console, or cut/paste as a sequence of commands into a console from an editor window. The script or its pasted commands may ...
0
votes
1
answer
183
views
gedit customisation via gsettings
I am trying to programmatically customise various settings in the gedit application, and currently I've got the following command to change the colour scheme.
gsettings set org.gnome.gedit.preferences....
0
votes
1
answer
68
views
shell fragment: compare against fixed string
I'm abusing Make to run some tests for me, and I need to compare the output of a command against a fixed string, ideally with a somewhat readable construct.
The best thing I can do if $SHELL is bash ...
3
votes
2
answers
523
views
"history" command ordered by most common
Can I get the output of history sorted in order of most-used?
I know how to do it in a programming language, but not from the shell. In my case I'm on macOS with zsh.
I know I could use uniq and sort ...
1
vote
1
answer
198
views
Ubuntu 22.04: I cannot start my browser automatically once logged in
Basically I want to login in my system and let the system to open for me my Web browser (Thorium).
Since I' using Ubuntu 22.04 and I want to start the browser as user (and not as root), I followed ...
0
votes
1
answer
126
views
Display only specific text in bash output
I am making a system info script which will output what version of CentOS a machine is running.
Currently, I have the following:
#!/bin/bash
distro=$(cat /etc/redhat-release)
echo "OS Version: $...
0
votes
1
answer
59
views
Count files in a directories AND create NEW folders based on that number
I have several directories that all have files in them. I want to count the number of files and then create subdirectories based on that count.
Example 1:
Main Directory
+-> Subdirectory
+->...
1
vote
0
answers
109
views
mdadm in script produces nonsesnsical error when muting output to /dev/null
Some really weird Voodoo magic is going on with mdadm in my script:
Creating RAID with: mdadm --create --run --level=1 --metadata=1.2 --raid-devices=2 /dev/md/4 /dev/nvme1n1p4 /dev/nvme0n1p4
mdadm: /...