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.

3
votes
2answers
298 views

NUL delimited variable

GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu) Idea is to set a variable to a NUL delimited data set. Here $samples This, however, result in: warning: command substitution: ignored ...
1
vote
3answers
238 views

Why does a file move/copy function only move one file at a time when using the “*” wildcard?

function mv1 { mv -n "$1" "targetdir" -v |wc -l ;} mv1 *.png It does only move the first .png file it finds, not all of them. How can I make the command apply to all files that match the wildcards?
0
votes
1answer
59 views

Recursive shell call

I have a use case where Dummy.sh load data and fix.sh fix problematic data and load data again. Here I have to implement retry logic, if fix script is not able solve problem in 3 attempt. But its ...
0
votes
2answers
62 views

grep files with pattern A but exclude files with pattern B

I want to grep files which include pattern A (iwant) but I want to exclude files which contain pattern B (idontwant). Example: read -p "...what are you looking for: " iwant read -p "...what should ...
0
votes
1answer
40 views

nested case statement in shell script

Below is the list of files i've in my current directory: ctc.SNAPSHOT-1.0.1-abc.gz ctc.SNAPSHOT-2.0.1-bcd.gz ApprovedList.A ApprovedList.B cat ApprovedList.A BC DEF I want to pass 3 parameters to ...
0
votes
0answers
48 views

Run script in background using `&` or `()&`?

If I want to run a script in background, for example a.sh, should I run ./a.sh & or should I put it in a subshell then background it? (./a.sh) & I test both and don't see difference, both ...
1
vote
1answer
28 views

How do I return octal from Bash's arithmetic expansion?

As clearly spelled out in the Bash documentation on shell arithmetic, if you prefix a number with a 0 within an arithmetic expansion expression (e.g., $(( expr ))), it's treated as octal. Similarly, ...
0
votes
0answers
15 views

How can I check that Cinnamon is in Tile mode in command line?

There is a window selection mode, or Tile mode in Cinnamon, enabled with ctrl+alt+down_arrow by default. In my bash script I need to check that this mode is currently active (user pressed shortcut ...
0
votes
1answer
85 views

Filename too long error in bash

I am storing a url inside a variable in bash. It has nothing to do with real file paths but it keeps giving me the error File name too long. How do I tell bash to ignore looking for filenames and ...
0
votes
1answer
30 views

TMOUT within script file misbehaves while it works in shell prompt fine. How to make it work?

UPDATE - looks like this is something peculiar with bash 4.3.42 as it works fine with 4.3.46. Leaving this post for those who run into the same issue in future. When I run this command in bash ...
1
vote
1answer
33 views

How to exit a shell script on error AND message the user?

I have a /bin/foo shell script file. How to exit a shell script on error AND message the user? If I simply use a set -e then it exits on error, but no commands running when it hits error, like ...
1
vote
1answer
37 views

Writing and Executing Program to behave like console

I have written a set of programs with the intent of using a radio transmitter-receiver (NRF24L01) to connect two devices as if they were connected via a serial interface. Currently, i am able to ...
1
vote
2answers
41 views

Rename + Move Nested Files in Bash Script

I have a directory users which is full of folders named for each user. Within those folders, there is a file called userinfo.csv which I would like to pull out of the folder and into the users. Since ...
1
vote
1answer
21 views

Bash | Txt file > tmp> process tmp> ouput

recently we got Jump Host to interact with all our devices. It's red had 6.x and we have no root access. We can script on Autoexpect and bash only - no modules possible. Source: I have 5000 devices (...
0
votes
1answer
15 views

Mv files listed in txt file up a directory [duplicate]

This is my first time asking a question :) I am new to bash scripting but pretty good with python. I have a directory with a bunch of files, and I have a text file that contains a subset of file names ...
0
votes
0answers
26 views

Add bash variable as JSON key and value into object

I have a JSON array of objects like so . [ { "id" : "tmp1387816934708382026", "owner" : "john", "x11-display" : ":5", "x11-authority" : "/run/user/john/dcv/tmp1387816934708382026....
2
votes
3answers
201 views

Alternative to Ctrl-R reverse search in bash

I am happy and really like the Ctrl-R backward search feature of the bash shell. Some of my colleagues don't like it, since it is sometimes confusing. I understand them. If you enter the wrong ...
-1
votes
0answers
28 views

How to add add additional columns to an sql insert query using shell script?

Example: $ cat data.txt insert into "APS"."HEARTBEAT"("adress,"date") values ('LIVE LIVE-22.',TO_DATE('03/15/2019 23:00:05', 'MM/DD/YYYY HH24:MI:SS')); Desired output: insert into "APS"."HEARTBEAT"(...
-1
votes
2answers
87 views

How to test file permissions using shell script?

I am testing if my file has read permissions, by running this script as root: #!/usr/bin/env bash FILE="/etc/shadow" if ! [ -r $FILE ]; then echo "Hello" fi exit 0 Ideally, script has to print ...
18
votes
1answer
1k views

bash does not know the letter 'p' [duplicate]

My bash does not know the letter p. zsh does though. When I type a p in bash, it simply ignores it, and even pasting in text that contains a p will result in no p in the pasted text. This behavior ...
0
votes
1answer
59 views

How to “export” functions in bash?

I have a script, which has a lot of functions inside. myfile1.sh: func1() {} func2() {} I can call them from script with: myfile2.sh: source myfile1.sh func1 func2 But how can I call them if ...
3
votes
1answer
75 views

why is `test.sh & | test.sh` wrong

I have a bash script named test.sh as below: #!/bin/bash while : do echo xxx sleep 1 done I don't know why ./test.sh & | ./test.sh gives me an error: -bash: syntax error near unexpected ...
1
vote
0answers
64 views

What could cause this trap on EXIT to never fire? [closed]

echo ' *** Site up, starting tests.' cleanup() { rv=$? # kill server and mock data server at the end kill $(lsof -i4TCP:3001 -sTCP:LISTEN -t) &>/dev/null; kill $(lsof -i4TCP:3003 -sTCP:...
4
votes
3answers
1k views

Why does ''cat "${1:-/dev/stdin} | … &>/dev/null'' work in bash but not dash?

Script: #!/bin/sh # # reads stdin/file and copies it to clipboard # clears it after 30s # cat "${1:-/dev/stdin}" | timeout 30 xclip -i -selection clipboard -r -verbose &>/dev/null & I ...
0
votes
1answer
29 views

Exit chroot from script

My script creates a chroot cage to install GRUB to a USB, run as sudo of course: SYSTEM_DIRS=(etc bin sbin var lib lib64 usr proc sys dev tmp) boot_partition=/media/user/boot for dir in ${...
0
votes
1answer
44 views

Make multiple directories from Mediainfo information to put music in

I have a bash script that goes through my music folder, gets all the song metadata with mediainfo and is intended to make folders out of all the artists' names, but it fails. This is the code so far: ...
-1
votes
2answers
31 views

at not executing scheduled command - troubleshooting

I am writing this bash script, that will read a file containing a date, a time and a phone number, and it will use an SMS provider API to send an sms reminder. #!/bin/bash while read date time phone ...
0
votes
1answer
22 views

Trying to parse a string in BASH for [ - _ ]

I have a BASH script that allows a user to specify a jobname. Right now we except everything. We feed this jobname into another executable that can only accept the following formats: job_name, ...
-1
votes
1answer
30 views

simple systemd service for execution of shell script

Im trying to start a simple .sh file and it says Failed at step EXEC spawning /etc/start.sh: No such file or directory Content of the gnome.service [Unit] Description=Description for sample script ...
2
votes
1answer
34 views

Why doesn't pattern substitution work from a single variable?

Please see the below session transcript: $ mkdir temp $ cd temp/ $ touch file{1..5} $ ls file1 file2 file3 file4 file5 $ SRC=file; TGT=ram $ for f in file* ; do mv $f ${f/$SRC/$TGT} ; done $ ls ...
0
votes
1answer
37 views

I want to take output of first command as a input of second command in shell [duplicate]

ls -t1 | head -n 1 will give me the latest updated file so I want to tail that latest updated file in shell. How do I do that?
8
votes
5answers
1k views

Why {1,2} printed by a command in $() is not interpolated?

I am in a directory in which I have two text files: $ touch test1.txt $ touch test2.txt When I try to list the files (with Bash) using some pattern it works: $ ls test?.txt test1.txt test2.txt $ ...
0
votes
2answers
52 views

How to write routine to escape double quotes in a JSON string [closed]

I have to be efficient so I can't use tools like jq which loads up a big binary executable. I just want to escape double quotes in a string so it's safe for JSON. This isn't good enough: echo ' bad \"...
0
votes
1answer
16 views

Explaining xargs output from two find commands

I have a directory structure of folder1 folder2 folder2a folder2b folder3 folder3a folder3b I am confused by the xargs output of the below command: XXXXXX:folder1 user$ find . -type d -...
0
votes
1answer
35 views

Use of IFS in “for loop”

lis="a:b:c:d:" IFS=: If I run the following code, for i in "a:b:c:d:"; do echo the output is $i done I get: the output is a b c d If I replace "a:b:c:d:" with $lis instead: for i in $lis; ...
2
votes
1answer
46 views

How can I make a script that opens terminal window, executes commands in it and remains open on Scientific Linux?

I'm working in a remote server that uses Scientific Linux (version=7.6 (Nitrogen)). I did a simple web application in Python3, and I found myself constantly opening a Mate Terminal (though, any ...
1
vote
2answers
43 views

How to split string by string (multi-character) separator into an array?

Basically I'd like to split a string into an array by a string separator like sep. for example. Is there native bash way to do this without using tools like sed, awk, etc? I know I can split a string ...
2
votes
1answer
83 views

Shell script returning “Running: command not found”. Not sure why

When I am setting a variable to a command, I am getting the answer along with the error "command not found". Below is the command in the script. #!/bin/bash fs=$(ps -C "daemon_name" > /dev/null &...
0
votes
1answer
28 views

Use sed to append data BEFORE a closing double quote encasing a Path

I need to append the path /opt/dell/dcc of an application outside of the default path statement in /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/...
-1
votes
1answer
31 views

How to Force String Type on a Variable?

I have this script: #!/bin/bash USERS=("user1.com" "user2.co.uk" "user3.com") PASSES=("pass1" "pass2" "pass3") for i in ${USERS[@]}; do echo ${PASSES[i]}; done running this script outputs: ...
0
votes
0answers
11 views

Why is my array declaration incorrect? [duplicate]

I'm trying to do some shizzle with SFTP and SSH but it would seem I'm getting hung up on super-simple array declaration. According to this, I should be able to use the following syntaxes to create an ...
0
votes
2answers
76 views

Check if named pipe is open for reading

Right now, I have this crappy check to see if a named pipe is being read from: is_named_pipe_being_read() { local named_pipe="$1" echo "unlocked" > "$named_pipe" & pid="$!" #...
4
votes
2answers
97 views

SHELL environment variable still points to zsh after using bash

I am trying to jump between zsh and bash. By default, I have zsh as my shell, I can know this by typing: echo $SHELL and I get /bin/zsh However, I want to open Bash, so I type /bin/bash; I assume I ...
1
vote
2answers
24 views

Check processes of multiple servers in parallel with if statement

I have a bash function which checks if there are any process running on servers by reading their IPs from a text file one by one while read IP do if [ 1 -eq "$(echo "$(ssh -n ubuntu@$IP "top -...
0
votes
1answer
28 views

Bash - How do I pass in parameters using an array?

I am trying to run a program with many parameters as follows: ./some-program -c 1 -c 2 -c 3 ... How should I create such a command in Bash shell scripting?
2
votes
1answer
31 views

How to check for presence of named pipe on the file system [duplicate]

I tried using the -f flag to test if a named pipe is present if [[ ! -f "$fifo" ]]; then echo 'There should be a fifo.lock file in the dir.' > /dev/stderr return 0; fi this check does not ...
1
vote
2answers
42 views

how to change the path of vim command

I have two versions of vim installed on macOS: a default version, /usr/bin/vim, and MacVim (via Homebrew), /usr/local/bin/vim. When I type vim, the default program is invoked. I want to use MacVim ...
2
votes
1answer
46 views

Redirect echo in trap function back to stdout

I'm currently having an issue getting echo statements in my trap function to be printed to stdout. I'm redirecting all output (error and stdout) to a log file from a command that I run. But if it hits ...
-1
votes
2answers
34 views

Why doesn't back-slash yield non-zero exit code

If run: true \ false; echo $? I get an exit code of 0. Does anyone know why that is?
-1
votes
1answer
14 views

multi-line export without a newline

How can I export something that's multiline? export foo='bar a ab ai ii bar' and put that on two different lines without a newline literal? export foo='bar a ab ...