Questions tagged [bash-scripting]

Bash scripting is making scripts in the Bash shell language.

Filter by
Sorted by
Tagged with
0 votes
0 answers
156 views

what is wrong with the spaces in this string?

i have following string (a version number): 2.2.0 (15). but the problem is, I can't trim the whitespaces. are there some curious whitespaces in bash? because if I do this: local test=" ${var} ...
m1well's user avatar
  • 135
-2 votes
1 answer
1k views

List/Print oldest and newest file in each directory

Could use a bit of help guys. I need to produce a report for the oldest and newest files that are residing in each unix directory The structure is simple Main folder with multiple subfolders that ...
Tom Tom's user avatar
0 votes
0 answers
288 views

Bash check array membership with regex

I have just started bash scripting as of this morning, so forgive me for my less-than-adequate skills. I have a GitHub action where I scp files which have changed on a push event to the production ...
Noah Broyles's user avatar
1 vote
1 answer
339 views

Move files to new folderif string matches

I'm trying to go through a number of folders and check their contents. If one or more of the files inside matches any of the strings in a given list, I would like to move those files to a new folder. ...
Oiko's user avatar
  • 11
0 votes
1 answer
127 views

Extract all emails from a text document

I have a document that contains text and HTML tags, and there is are a lot of tags like <label>[email protected]</label> How do I extract all the emails from this document using Linux commands....
daniel's user avatar
  • 101
0 votes
0 answers
55 views

Variables inside commands inside if statement

I do not understand why this does not work: existing_user=$(ldapsearch -x -b "cn=group,ou=group2,dc=company,dc=com" -H ldap://127.0.0.1 -D "cn=admin,dc=company,dc=com" -w "...
Belphegor's user avatar
0 votes
1 answer
720 views

Modify user in openLdap via bash script

Can you please help me put this command into if statement (bash scripting): $ ldapmodify -D -h password: [enter password] dn: cn=vipb,ou=groups,dc=example,dc=com changetype: modify add: memberUid ...
Belphegor's user avatar
1 vote
1 answer
2k views

passing bash variables to expect script or scripting expect from inside bash

This script is intended to mount the contents of my server at home as a read/writeable drive in mac os. Unfortunately due to the fact that my house has a dynamic public ip address, the ip address of ...
Christian Maxwell's user avatar
1 vote
2 answers
61 views

Creating multiple symlinks from a string

I have the following script: #!/bin/bash test="../t1,../t2,../t3" ln -sf "{${test}}" . Above this directory consists of 3 directories: ../ ├── script │   └── test.sh ├── t1 ├── ...
dogman's user avatar
  • 13
1 vote
1 answer
42 views

bash search and replace replaces extra character

I am trying to use bash' Parameter Expansion to replace a dollar-sign and am having trouble. I've tried variations but nothing I've tried works, what am I doing wrong? Example: in_amt="($12.34)&...
Senior Geek's user avatar
0 votes
1 answer
441 views

Killall -9 chrome, but not chrome-beta

Both Google Chrome and Google Chrome Beta are named "chrome" in the process list. I want to kill all instances of chrome, but keep all instances of chrome beta;) Any pointers as to what ...
Jason Hunter's user avatar
0 votes
1 answer
3k views

removing old BTRFS snapshots with bash

I'm trying to build my own little backup script, I want to use BTRFS snapshots and keep them for a while. Meanwhile, creating the snapshots is no longer a problem for me, but now I would like to ...
FrankStein's user avatar
0 votes
1 answer
866 views

how to insert a phrase after a word with sed in mac

I have the following code: sed -i "/#alias/a alias ll='ls -laGh'" /etc/zshrc sed -i "/#alias/a alias l='ls -l'" /etc/zshrc in the file /etc/zshrc I have: #alias but when ...
zeros's user avatar
  • 271
0 votes
0 answers
674 views

Bash script treating a multiple arguments like a single argument. Need multiple arguments as multiple arguments

Like "sudo" I would like to create a command that lets me test if the web page can do things like ping, wget, cat, etc. This bash script works OK for single commands like 'www ./test.py' but ...
Klint Krossa's user avatar
0 votes
1 answer
354 views

How to bulk delete zip files without extention in linux

I have a folder full of different files but without extension. How can I delete all files that are zip files?
Ramy's user avatar
  • 3
0 votes
1 answer
1k views

match a string with a whitespace in bash

I have the following string with the following path: #!/bin/bash FICHERO_MARCADOR="/Users/javier/Library/Application\ Support/Google/Chrome/Default/Bookmarks" cp -p $FICHERO_MARCADOR ./...
zeros's user avatar
  • 271
0 votes
1 answer
756 views

Trouble with a bash script running as a systemd service restarting another systemd service the first time

I've written a bash loop and named it freud.sh. freud.sh #!/bin/bash set -x while : do sleep 30 systemctl stop pacemaker sleep 30 systemctl start pacemaker done Here is my unit file: [Unit] ...
choseph's user avatar
4 votes
1 answer
3k views

How to download a portion of a YouTube video

I often come across YouTube videos that I need to crop parts from, but these videos can be long and downloading the entire video just to process a small part of them would take too long and be very ...
Hashim Aziz's user avatar
  • 12.8k
0 votes
1 answer
65 views

Using `-to` instead of `-t` when using YouTube link as input

This answer shows how to get a portion of a YouTube video by using URLs as input. The command works mostly fine, but I want to avoid using -t as I need to cut between specific timestamps and I'd ...
Hashim Aziz's user avatar
  • 12.8k
0 votes
2 answers
3k views

Using read to capture multiple variables from a command's output

I have a youtube-dl command that when run outputs two new lines, and I'm trying to capture each of those to variables. Here is what I have so far, but for some reason I can't understand, it only ...
Hashim Aziz's user avatar
  • 12.8k
0 votes
2 answers
36 views

if statement - simple bash scripting

I am working on a simple script that checks if there is .vimrc file and what is configured in that file: if [ "$(ls -a /root | grep .vimrc)" = ".vimrc" ]; then echo "...
Belphegor's user avatar
0 votes
1 answer
144 views

Bash script special character

I am learning bash scripting, and I found this command in the book: case `date +%a` in Can someone explain what does + and a mean? I found that the % sign is there to check our output from the left to ...
Belphegor's user avatar
0 votes
2 answers
1k views

How to check in bash if a function's arguments does not contain any parameters

"Argument" vs. "Paramter" First of all, let me clarify what I mean by: if a function's arguments does not contain any parameters Given the following ls command: $ ls -a ...
winklerrr's user avatar
  • 366
0 votes
1 answer
165 views

Cause of lagging process execution

I am running two programs. One python program which generates a set of large files and writes them to an external hard drive I have mounted to the computer. The second is a bash script which cleans ...
GTOgod's user avatar
  • 109
0 votes
1 answer
61 views

Reject users script help - bash - freeradius

I have this very small script that allows users to access defined hosts, but I do not know how to reject users to access everything else: This part of the script works fine: 318 Huntgroup-Name ...
Belphegor's user avatar
0 votes
1 answer
884 views

Perform single action for every subdirectory

I am using git to manage my repositories. it can perform action only to the current directory/repository. I have directories structure like the following: myRepos | --repo 1 --repo 2 --repo A --...
Omtechguy's user avatar
  • 163
1 vote
1 answer
2k views

Starting X Server

I installed Arch with KDE plasma. I want to start KDE without display manager and configured files by instructions from Arch wiki https://wiki.archlinux.org/title/KDE#From_the_console .But after ...
user avatar
0 votes
0 answers
95 views

Why does Bash require closing some statements?

Something I've always wondered but never gotten around to asking. In Bash: if statements require an ending fi case statements require an ending esac do statements require an ending done (which ...
Hashim Aziz's user avatar
  • 12.8k
4 votes
2 answers
16k views

When pressed ctrl+c only break out of current function and not whole bash script

I'm trying to write a script wherein if someone pressed CTRL+C (Keyboard interruption) then it shouldn't exit the entire script, just the current execution function. Can anyone suggest what logic can ...
m4xx's user avatar
  • 65
2 votes
4 answers
190 views

How to rewrite a wall of if-elif statements

For cigs.sh - the complete script can be found here - I wrote the following ugly (but perfectly working) logic to print and format the script's output, partly just to figure out all the edge cases but ...
Hashim Aziz's user avatar
  • 12.8k
0 votes
3 answers
564 views

How to display weeks, days and hours in script

This is a follow-up question to a previous question regarding a bash script I wrote to help me more accurately keep track of my vices. I'm now trying to modify the script to display all non-zero units:...
Hashim Aziz's user avatar
  • 12.8k
4 votes
1 answer
5k views

How to make getopts parameter optional

I wrote the following bash script as a budgeting tool to more accurately calculate (and moderate) when I last bought a pack of cigarettes. In addition to -h for printing the output, it takes one other ...
Hashim Aziz's user avatar
  • 12.8k
1 vote
1 answer
40 views

Matching variables in bash

Trying to ping IP and return result with the HOSTNAME. first ip is responsive second is not, but the script prints all HOSTNAMES How do I match IP (i) with the HOSTNAME (h)? Tried: for i in 10.10.63....
user1349083's user avatar
1 vote
1 answer
88 views

Access positional parameters out of order in a for loop

I have a script which makes use of multiple (i.e. positional) arguments from the command line, and within it a for loop that iterates through these arguments: for i in "$@"; do ... done ...
Hashim Aziz's user avatar
  • 12.8k
0 votes
1 answer
855 views

How to access positional parameters from within a for loop

My shell script contains a for loop. The script makes use of positional arguments via $1, $2, $3, etc. However, trying to access these parameters from the for loop using something like this: for i in &...
Hashim Aziz's user avatar
  • 12.8k
0 votes
1 answer
209 views

Copy a jpg file to another folder bash script unix

The code I have written has some errors: cd OS2/OS22 for f in *.jpg do cp -v "$f" OS! "${f%.jpg}" done It says: cp: target 'photo' is not a directory. *Can someone please ...
Klea's user avatar
  • 3
3 votes
1 answer
27k views

What is awk '{ print $2; }' doing exactly?

I would like to know what is this statement doing exactly, especially the awk part. ps aux | grep 'sidekiq 5' | grep -v grep | awk '{ print $2; }' | xargs kill -${2:-'TERM'} I know the entire ...
Mohsin Sethi's user avatar
0 votes
1 answer
51 views

a very simple Perl script needed

I have the following text file G.txt. I need a perl command perl -e ... that would extract all negative entries ending with Kč, so that first line it will be -107,60 and last will be -63,00 I have ...
user113823's user avatar
1 vote
1 answer
1k views

Bash: Open new tab in newly opened terminal

I want to open new terminal windows, each with multiple tabs, and while I've managed to open the windows, I'm having trouble with the tabs. What I currently have is: #!/bin/bash # Opens new window, ...
vic's user avatar
  • 111
0 votes
0 answers
115 views

How do I copy file contents to a variable while using sudo?

I have a file that only has root permissions. I need to copy its contents into a variable in my script, but when I do content=$(sudo cat secretfile.txt) it captures the "last login" ...
YellowElmo's user avatar
0 votes
1 answer
3k views

Shell Script : Take filename as an argument and on a standard output display the contents of the file with line number prefixed to it

So, I'm new to shell scripting and I'm trying to write a shell script which takes filename(.txt) as an argument and displays the line number prefixed with each line on a standard output. This is the ...
saurabh's user avatar
0 votes
1 answer
94 views

VPN Email Alert by IP address (Bash Script)

I have an OpenVPN server, when a user connects to the server they are given an IP from 192.168.1.20-25. I want to get an alert when these IP's reply back as well as when they disconnect. So far I can ...
Froggy's user avatar
  • 1
0 votes
1 answer
900 views

Why does my bash script not cd correctly?

I'm using Windows Subsystem for Linux, and learning the terminal. I want to open applications on my desktop with the terminal, so I decided to make a bash script to cd to the .exe file and run it. ...
ZackT's user avatar
  • 93
1 vote
1 answer
185 views

Bash not doing word expansion after command substitution

From GNU's Bash Manual Sections 3.5.7: Word Splitting: The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for ...
Jonathan Ginsburg's user avatar
0 votes
0 answers
66 views

Split a file into multiple files based on values from bookkeeper file

I have 2 files, where I need to split File 1 into multiple files of size specified by File 2 contents. Sum of File 2 contents will always be equal to size of file 1. There can be many entries in File ...
ramuloramula's user avatar
0 votes
1 answer
99 views

Print the files if exists from different folders with same suffix integer value in shell script

I have two different folders Folder1 Folder2 cmp_data_0.bin decmp_data_0.bin cmp_data_1.bin decmp_data_1.bin cmp_data_89.bin ...
ramuloramula's user avatar
0 votes
1 answer
77 views

BASH: List External drives

I am trying to make a script that list all disk on my Mac exluding /dev/disk0 and /dev/disk0 (I only want it to show the external and virtual drives) and the name of the primary partition. Something ...
Luismm2311's user avatar
0 votes
1 answer
807 views

How do I replace a certain line in /etc/passwd file?

So, I'm trying to make a script that disables the root account by changing the root:x:0:0:root:/root:/bin/bash with root:x:0:0:root:/root:/sbin/nologin. Is there a command that lets me do this? Thanks....
Bottish's user avatar
  • 317
1 vote
1 answer
171 views

Bash script that displays time on terminal

I wanted to write a bash script that serves like a greeting and show date and time(in terminal), and I ran into some issues. I've done some digging I solved most of the issues. The one and only issue ...
black-purple's user avatar
0 votes
1 answer
475 views

Script runs slow processing stdout piped from service

I am using a utility named oscdump which listens on a specified port for OSC messages and prints the messages to stdout. I wish to parse and process these messages in a script however my script runs ...
sebinho's user avatar
  • 73

1 2
3
4 5
20