Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [bash]

Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead.

0
votes
3answers
40 views

Oneliner for outputting to file only if string is matched (bash)?

I am making a script where I curl a URL and output to a file like this: curl http://example.com/$1 > $1 Is there any way to make it dismiss anything that doesn't include, say <head>? I ...
1
vote
1answer
138 views

Bash prompt getting garbled when I browse history?

I have MacOS and .bash_profile content: export PS1="\[\e[0;31m$\]\[\e[m\] \[\e[0;32m\w\e[m\] : \]" as a result I have pwd printed in terminal like this: but when I press up and down arrows to use ...
0
votes
1answer
23 views

Bash: Error in assigning default value to a variable

Inside my bash script: This works: CWD="${1:-${PWD}}" But, if I replace it with: CWD="${1:=${PWD}}" I get the following error line #: $1: cannot assign in this way Why I can't assign to ${1}?
0
votes
1answer
68 views

How to write Bashrc with and without sudo for different use cases? [closed]

I'm searching through a ton of packages between a vm and host to see which package is causing text input issues for steam. I frequently need to check for and install packages on the guest machine and ...
0
votes
1answer
19 views

Running multiple commands using one file

I have a file with listed commands: first is the script name, like: get_1000g_r2_prevcf_snplist_e.pl and everything else is argument to the script. Here is a couple of lines in the major file which I ...
1
vote
0answers
23 views

Shell Scripting best practice to make sure script is not over utilizing memory or CPU [closed]

I have been writing shell scripts from long time but I never thought of optimizing script to make sure its not using more cpu or memory .. I am looking out for some suggestions or process with which I ...
0
votes
1answer
26 views

Merging PDF files with similar names

I have a folder populated with PDF files in this format: SI-T-xxxxxx.pdf SI-T-xxxxxx-sol.pdf SI-T-yyyyyy.pdf SI-T-yyyyyy-sol.pdf and so forth, with the middle part being digits. I want a way for ...
0
votes
3answers
43 views

How to use wildcard * with 'A long filename'?

I'm trying escape characters like \' but it doesn't work. $ cp 'A long filen* ./short_filename
0
votes
1answer
64 views

Remove file-name extension, if it exists

Very new to scripting. I have a directory containing some text files and some with text files that also contain an additional extension (.xfr) e.g. file1.txt, file2.txt.xfr, file3.txt, file4.txt.xfr. ...
1
vote
2answers
28 views

Speeding up repeated python calls (or, alternatively, porting a complex regex to sed)

I am an academic medical physicist. I do experiments that generate a fair amount of data, and they are expensive to run. My university has a backup system that consists of a robot tape library in a ...
1
vote
0answers
22 views

Correct way to quote variables for use in Bash backticks? [duplicate]

I have a script which I want to go over 4 sets of files PURO*.xml, PURR*.xml, SALO*.xml and SALR*.xml. for j in PUR SAL do mask=("${j}O*.xml" "${j}R*.xml") for k in 0 1 do ...
0
votes
1answer
34 views

Output string to log file, then pipe to function as argument

So I am trying to echo a string out to a log file and also pass that echo'd string over to a function along with other arguments as well. I can't seem to figure out how to do it or find any answers on ...
1
vote
1answer
35 views

Desktop Entry Requires “bash” in Exec Value

I recently began messing with Desktop Entries in order to run some scripts at Gnome startup. I've read through some freedesktop documentation, as well as this post on creating a startup script. I ...
1
vote
1answer
30 views

Login script that requires user to re-enter displayed random number string

I currently have a script that runs upon a user logging into a Linux server and at the end it prints [Terms of use agreement prints here] read -p "Agree and continue..." -nl -s However, most users ...
1
vote
2answers
35 views

bash: declare/typeset without assignment appears to fail

Using bash 4.3, there seems to be a discrepancy between the manual and actual behaviour: unset string # just to be sure declare string # $? is 0 afterwards declare -p string # fails, 'string: not ...
0
votes
1answer
38 views

Find, grep and then create table

I am trying to find all *.md at ${PWD}, then grep for lines beginning with title: and print it along with the absolute filepath -H and the line number of the pattern match -n, then I try to output the ...
1
vote
2answers
31 views

bash remove duplicate lines from txt files in folder

I have this command to remove duplicate lines from a file and save the unique lines in a new file uniq telphone.txt output.txt However Im trying to find a command that scans all .txt files in the ...
-2
votes
0answers
44 views

Calculate percentage from two date/time stamps bash

I have two files "DURATION.txt" and "ABCDURATION.txt" which contain ~900 records where DURATION.txt is the total runtime and "ABCDURATION.txt" is a portion of that total. What I need to do is ...
0
votes
1answer
28 views

bash: ucbps: command not found

How do I solve this? bash: ucbps: command not found [cbwlskbd@cbl06677app08 ~]$ Where to include the path for this? and how the paths are included
0
votes
2answers
26 views

Renaming part of name of all files in a directory tree if match

I have a directory tree like this . |-- players | |-- red_alice.plr | |-- red_bob.plr |-- resources | |-- red_cash.rsc | |-- red_food.rsc I want to go through all directories and rename any ...
0
votes
1answer
33 views

How can I check if the number of arguments passed into the shell script is exactly 2?

I have some understanding that to check the number of arguments being passed into the shell script all I have to do inside the shell script is this: Example 1 if [ "$#" -ne 2 ]; then do something; ...
0
votes
1answer
19 views

Stopped process reasons?

I'm running a program using a batch file and it keep giving me this ERROR: STOPPED PROCESS I've noticed that the program stops each time in a place a little after the place he stopped the last ...
0
votes
1answer
21 views

How does bash coprocess achieve its pipelining?

Note this passage from man bash ( emphasis mine ): Coprocesses A coprocess is a shell command preceded by the coproc reserved word. A coprocess is executed asynchronously in a subshell, as ...
0
votes
1answer
19 views

Converting rows of column data to matrix using datamash

I have a file which looks like this 0 1 1 2.3 0 2 2 3.1 0 3 4 1.3 0 4 5 2.5 0 5 6 7.1 1 1 1 3.3 1 2 2 1.1 1 3 4 2.3 1 4 5 4.5 1 5 6 6.1 2 ...
2
votes
1answer
30 views

use “$n” as a string bash variable in an awk script?

I have an awk script in a bash script that I am starting to generalise. I would like to put the string "$3" into a bash variable and use that variable in the awk script. This would allow me to easily ...
0
votes
1answer
37 views

Bash OR operator

My script is working with this: if pgrep -f "/home/tiger/bin/pymp" >/dev/null 2>&1 ; then I want it to check for mpv2 also. This is what I have so far: if [[ /usr/bin/pgrep -f "/home/...
2
votes
3answers
59 views

Awk: Count occurrences of a string in one column, between a range of lines starting 2 lines below pattern 1 and ending with a condition

I have an input file with several thousand lines. I am interested in one section of it, which contains the first instance of pattern /m.o./ in the file. I need to search for this pattern, run my code, ...
9
votes
3answers
601 views

“Variabilize” the ampersand (background a process)

I want to know if there's a way to put the ampersand in a variable and still use it to send a process to the background. This works: BCKGRND=yes if [ "$BCKGRND" = "yes" ]; then sleep 5 & ...
0
votes
1answer
76 views

A specific directory is completely invisible to “ls” and “ls -la”, but I can “cd” into it

This started happening out of the blue. Originally, I was able to "ls" my "Storage" folder and all 3 subdirectories were listed accordingly. ('Beta Profiles' 'OS Backups' 'Software') Starting ...
0
votes
0answers
58 views

How to package a Bash program for installation in Linux

I have a program written completely in Bash, which includes the main script, a set of modules called by the main script, a config file, a lib file, etc. Since nothing needs to be compiled, what's the ...
0
votes
0answers
17 views

Using xargs -n and -I together [duplicate]

I have 600 files I wanted to copy to S3. Wanted to do it in batches of 10, so I wrote: $ ls -1 | xargs -ILST -n 10 echo s3 cp LST xx s3 cp LST xx f9b3ac7e-3683-42dd-aed3-41b36bcf822c f9c30a0d-c196-...
1
vote
1answer
18 views

cycle through reverse search in a custom command history file in bash

I added a line to ~/.bashrc file which appends all issued commands to ~/.bash_eternal_history file in a date directory history-number command format, such as: 2019-01-08.23:59:32 /home/sport/...
2
votes
1answer
42 views

How to create a bidirectional pipe in bash?

I have a program which reads from and writes to file descriptor 3. I want to let it write to fd 3, and be able to write to the other end of the pipe interactively which the program should read over ...
1
vote
0answers
36 views

Pipe echo of associative array into dmenu

I have this script: #!/bin/bash declare -a arr arr+=( [mirror]="xrandr --output hdmi-1 --same-as edp-1" [extend]="xrandr --output hdmi-1 --auto" ) screen=hdmi-1 chosen=$(echo -e ${!arr[@]}| dmenu -...
1
vote
1answer
29 views

functions with touch [closed]

Why doesn't the following work? function xyz(){ touch $1; } xyz temp1.txt I get an error touch: missing file operand Try `touch --help' for more information.
2
votes
1answer
26 views

How can I build an env file (with $ characters) listing as-is from echo command?

I am trying to build a file invoked from a script that echoes what is in quotes to a file - only, the $ gets omitted. This is what I was trying to do: echo -e "export ORACLE_BASE=/oracle/APP/grid \n ...
1
vote
1answer
38 views

how to intercept the command that launches a bash script

If I run a bash script from the command line (with root permissions), is it possible to intercept the boot command within the script (while the script is still running) in order to log it? Thanks in ...
2
votes
1answer
47 views

What does the `-f' option do for `wait' versus the default behaviour?

Bash 5.0 includes a new -f option for wait:[1] j. The `wait' builtin now has a `-f' option, which signfies to wait until the specified job or process terminates, instead of waiting until it ...
0
votes
2answers
24 views

Repeat command execution attempts until status is not specific, preserve data

I have a case where a first few command execution attempts after daemon restart are returning 2 (followed by stderr messages), than everything works correctly. In order to bypass this bug I've come ...
2
votes
1answer
310 views

What is the purpose of BASH_ARGV0 in bash?

In the release for Bash-5.0 it is mentioned that they added BASH_ARGV0: a new variable that expands to $0 and sets $0 on assignment. What is the purpose of this? When is this an improvement over ...
1
vote
1answer
50 views

Testing if two directory trees overlap

Let $foo and $bar be two absolute directory names (output by realpath). I want to test if either is contained in the other one, at any depth, and execute commands accordingly. I thought I'd use find, ...
1
vote
1answer
35 views

Changing HISTSIZE in ubuntu 16.0.4LTS

I've been trying to change the HISTSIZE system wide and it hasn't been working. I've tried multiple different methods : adding /etc/profile export HISTSIZE=100 adding /etc/enviroment HISTSIZE=100 ...
1
vote
1answer
22 views

How to get clipboard as input file name to Vim on bash?

I install xclip to access system clipboard, my setting and some operations as below: ➜ ~ cat .zshrc | grep xclip alias c="xclip -selection c" alias v="xclip -selection c -o" ➜ ~ which dict | c ➜ ~ ...
0
votes
2answers
35 views

Check if any file exist in a folder when traversing through it

I have this script: local_dir="/some/dir1/" cd $local_dir for i in *.* do # do something ........ # [...........] rm $i done It throws an error when the folder is empty. *:...
2
votes
1answer
49 views

Write a script that accepts group numbers (GIDs) as parameters

I'm trying to write a script that accepts group numbers (GID) as parameters. The parameters can be any number. The task of the script is to calculate and display the number of users belonging to the ...
1
vote
1answer
25 views

bash curl post request set timeout

I make a post request from url like this curl -s -X POST http://site.com -d "data:data" The thing is that I want to set a timeout for the url I am sending the post request to. I tried -m 5, but ...
1
vote
0answers
23 views

Script get in process multiple times

I have a script that starts, kills or restarts several java programs. However, when I execute it, it ends up running multiple multiple times in the background. This is my script: #!/bin/bash export ...
1
vote
1answer
63 views

curl doesn't work in bash loop

I run this command in a single bash file to post the data to a url and get the response curl -s -X POST $url -d "username=username" It outputs the response body. All good But I want to automate ...
0
votes
0answers
37 views

Sorting before printing

I have the below block while read -r line do bad=$(cat node.txt | grep -A1 success | grep -vi success | grep -i '|2|' | sed 's/|2|/ /g' | awk -v count_num=$count 'FNR==count_num') good=$(cat ...
7
votes
3answers
336 views

Portable check empty directory

With Bash and Dash, you can check for an empty directory using just the shell (ignore dotfiles to keep things simple): set * if [ -e "$1" ] then echo 'not empty' else echo 'empty' fi However I ...