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.

learn more… | top users | synonyms

1
vote
3answers
70 views

Delimiter in word splitting

In Bash, word splitting is a step in command line processing. From Bash Manual The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words ...
2
votes
4answers
35 views

How to catch and handle nonzero exit status within a Bash function?

Say I have the following (pointless) Bash function: myfunc() { ls failfailfail uptime } I run it as follows: myfunc || echo "Something is wrong." What I want to happen is ls runs (as ...
0
votes
1answer
24 views

Correct syntax for inputting date variable into date calculation code

In my script, the following code successfully produces a timestamp variable for $n in the format 2016-04-28T15:47:48 for n in $(perl parsetime.pl | sed "s/.....$//") do echo $n result is: ...
1
vote
1answer
17 views

Trying to copy/paste long script with multiple EOFs, Terminal starts running half-way through

Was trying to create a text block that I could just copy and paste into a Terminal window to accomplish something (in this case, create a systemd script to do maintenance on MySQL dbs, create a timer ...
-1
votes
0answers
19 views

Bucket Brigade Help [closed]

I want to write two scripts, brig and passer. The brig fire n command starts a fire with a n size. The brig status command displays the status of the fire. The brig quit command just exits the fire ...
1
vote
1answer
36 views

Bash print current line, line's output, and linebreak to file

I've got a txt of a bunch of domains I need to run dig +short mx on. I've got a script set to run the command, print it to a results.txt with: ./dig_domain_mx.sh > results.txt Downside is, I ...
1
vote
1answer
58 views

How does Bash process a command line?

From Learning the Bash Shell by Newham: Each line that the shell reads from the standard input or a script is called a pipeline; it contains one or more commands separated by zero or more pipe ...
4
votes
2answers
39 views

Store quotes in variable to use as command line parameter Bash

I am writing a script to call a command (weka, if you are curious) a couple of times with some different parameters. One of the parameters, '-search "< stuff >" ' needs to be quoted. In some ...
2
votes
1answer
32 views

How do I check for the existence of a process without a failed exit code being returned?

I’m using Amazon Linux. I want to start a process provided one isn’t running already. This is a bash script I embed within a Jenkins Maven job. So I have pids=$(pidof /usr/bin/Xvfb) if [ ! -n ...
3
votes
3answers
185 views

adding special characters to folder names

I have a very long list of folders with the following naming convention ABS1789_2563-01 ABS1789_2563-02 ABS1789_2563-02 . . . How can I add " - " between ABS and 1789 then replace "_" by "-" ...
5
votes
1answer
48 views

Cannot grep jobs list when jobs called in a function

I can grep the output of jobs, and I can grep the output of a function. But why can't I grep the output of jobs when it's in a function? $ # yes, i can grep jobs $ jobs [1]+ Running vim ...
0
votes
0answers
19 views

Creating an iso file from a Dual layer dvd

I created an iso file using mkisofs "for linux OS: RH 7". Mounting the image shows the same folders of the original source from which the iso is generated. However when using this iso image to ...
0
votes
1answer
20 views

Dynamically write bash script from another?

I am trying to dynamically create and write to a file a bash script from from a current bash script. echo "## Get the IP of the system that was assigned #!/bin/bash ip=$( hostname -i | xargs ) echo ...
0
votes
1answer
20 views

Including milliseconds in a Script?

sudo hexdump -v -e '1/1 "%02x\n"' /dev/ttyUSB0 | awk '$1=="ed" { getline byte1 getline byte2 number = strtonum("0x" substr(byte2,2,1))+2 ...
0
votes
2answers
41 views

How can I forward #! through a bash script to provide indirection

Sorry if the question statement wasn't very clear. I'd like to have a bash script that I can edit to change the behavior of a bunch of other scripts. For instance--I have some groovy scripts. they ...
1
vote
2answers
22 views

Failed to open config file '/dev/fd/63', error: No such file or directory for wpa_supplicant

When I do this: sudo wpa_supplicant -D nl80211,wext -i wlp4s0 -c <(wpa_passphrase "some ssid" "password") I get Successfully initialized wpa_supplicant Failed to open config file '/dev/fd/63', ...
-3
votes
1answer
19 views

Code to calculate 15/30 minutes before specified timestamp (GMT) in ISO 8601 format

I am working with a timestamp which is in the following format which I am told is know as ISO 8601 : 2016-04-27T14:14:47.836Z I would like to be able to calculate the timestamp both 15 minutes and ...
1
vote
1answer
23 views

How to get pid of subprocess from the subprocess itself

How can I get pid of a subprocess from the subprocess itself? foo() { echo pid=$$ echo spid=$! echo bpid=$BASHPID } echo PARENT_PID=$$ foo & wait on first run I get: PARENT_PID=17112 ...
8
votes
4answers
541 views

Bash program not executed if redirection would fail

In bash, I notice that if a command using redirection would fail, any programs which run prior to that are not run. For example, this program opens the file "a" and writes 50 bytes to file "a". ...
4
votes
1answer
36 views

What does the history command !# do?

From the manual pages on history: Event Designators An event designator is a reference to a command line entry in the his‐ tory list. Unless the reference is absolute, ...
-1
votes
2answers
41 views

Use of && and || operators to handle the result of the same command

In bash, is it possible to use these two operators to handle both of the exit statuses of the same command ? or if I want to handle both cases, I have to use an if statement ?
0
votes
3answers
46 views

sed substitution

I have a string as follows: /test/test/ 12 /test/test My goal is to turn it into: /test/est/ 12 /test/test This is the command: echo "$x" | sed 's@\(/*/\).@\1@g'" I am using at the moment but ...
0
votes
2answers
8 views

SFTP suppress all messages except errors

I want to 'put' a file from local system to FTP server using sftp (only) from bash script. I want to suppress all messages except errors: sftp $user@$server 2>&1 1>/dev/null <<EOF ...
2
votes
2answers
31 views

Get specific result from function

Is there a way to return a specific value in an echoing function? return allows me to return an exit status for the function. I need to return a more sophisticated data structure such as an array, or ...
0
votes
4answers
22 views

Echoing piped output [duplicate]

Is there a way to echo output that being piped to the next command? For example lets say that I can reading filenames from a text file and then running a command on that file: cat files.txt | xargs ...
1
vote
2answers
43 views

cd to original location when recording session with “script” command

I have script log.txt in my bash profile; that creates a new sub process and records my entire session to log.txt. When I press cmd+t to open a new terminal usually this will open one in the same ...
1
vote
1answer
17 views

JSON with variable key in bash

In the bash script below, I have JSON object and need to have key hostname with random name, like host-$NAME (i.e. host-1, host-3). How to do it ? #!/bin/bash NAME=($(( ( RANDOM % 10 ) + 1 ))) ...
-1
votes
0answers
28 views

Shell Script- Conversion script for reading and updating data to new column

I am writing a conversion script that will help me to read the data from a column and move it to a new column. I want to read data row by row for column cross_reference and copy this data to newly ...
0
votes
2answers
46 views

Binding Timestamp with Hexdump?

I am running a script which is given below, sudo hexdump -v -e '1/1 "%02x\n"' /dev/ttyUSB0 | Many Pipes | gawk '{ print strftime(":: %s"), $0 }' Here the data is being hex dumped with timestamp ...
3
votes
2answers
36 views

Should suspended application in the background (by ctrl+z) still run or should it stop running?

I run Firefox with command xinit /path/to/firefox/binary and it opens a display with Firefox running on it. Then I push Ctrl +Alt+F1 to go back to console and then I push Ctrl+z to suspend the ...
-1
votes
0answers
41 views

backup script for apache [closed]

I got Apache installed on my CentOS and I'm trying to do the following as a routine checkup every time I run the script, which I will set up with cron. How can I fix the bash script? Task: 1st ...
2
votes
2answers
152 views

What's the fastest way to get n-th line of a text file [duplicate]

Given a file with m lines, how to get the n-th line. m can be smaller than n sometimes. I have tried: method1: sed -ne '10p' file.txt method2: sed -ne '10p' <file.txt method3: sed -ne '10{p;q;}' ...
0
votes
2answers
35 views

Sed replace not functioning as expected

I want to replace listen = 127.0.0.1:9000 by listen = '/var/run/php56-fpm.sock' sudo sed -i 's/listen = 127.0.0.1:9000/listen = \'/var/run/php56-fpm.sock\'/g' /etc/php-fpm.d/www.conf Error: bash: ...
7
votes
2answers
299 views

reason for exec in wrapper scripts

I have seen wrapper script examples which in a nuthell are following: #!/bin/bash myprog=sleep echo "This is the wrapper script, it will exec "$myprog"" exec "$myprog" "$@" As seen above, they ...
0
votes
3answers
33 views

Compress and remove original directory with limited storage

On my 3 TB external hard drive, I have a 2.7 TB directory containing relatively small files. I would like to compress this 2.7 TB directory and remove it to keep only the compressed version. The issue ...
0
votes
1answer
20 views

Bash script using mysql command with variables failing

I have a script to clone a vhost and associated db - however one of the mysql commands doesn't work. It fails and ends up outputting the mysql version number and 'how to use' info as if it hasn't been ...
0
votes
2answers
26 views

Having trouble deleting specific directories within a specific parent directory

I’m using Amazon Linux, bash shell. I’m trying to write a statement that will delete all child directories named “target” within a specific master directory. I would like to write things such that ...
0
votes
1answer
31 views

Vim saved file not updating on webpage

I am attempting to update an HTML file on my server, but when I do so it doesn't appear to be updating, but rather, serves an old copy instead. I went through the usual process of pressing :w and ...
1
vote
1answer
44 views

Creating a complex variable content by hand

I am creating a shell script dynamically and sending it to a remote server for execution. All is working except when I try to inject a variable with content coming from a file with multiple lines, ...
0
votes
0answers
27 views

What is the meaning of :- in unix? [duplicate]

Trying to understand the existing script where they have assigned ${3:-} to variable. Script : partition_parameter=${3:-} if [ -z $partition_parameter ]; then readonly drop_partition=false ...
1
vote
4answers
85 views

Dangers of using rm command with variables

I am creating a crontab that compresses 15 minute clips from my security camera into one file (24 Hours Long) and then having the clips delete. avimerge -o /media/jmartin/Cams/video/Full_$(date +%F ...
0
votes
1answer
82 views

Bash script commands execution wrong order [closed]

I have the following script: #!/usr/bin/env bash #Find the IP via ping ping=$(ping federicolivieri.noip.me -c 1) #Edit the ping output and grab the IP ip=$(echo ${ping} | awk '{print $3}' | rev | ...
1
vote
1answer
18 views

CentOS - change the password of GPG file by bash script

I am looking for a bash script which input is the old and the new password for changing password of a *.gpg file. I encrypted a txt file with command gpg -c. System create a file.txt.gpg. How can I ...
1
vote
1answer
25 views

How do I both censor a command away from .bash_history and also be able to return to it with up arrow?

When one prepends a space character to the command, it disappears from history. But this also causes the command to no not easily editable and re-runnable. How do I make weaker version of ...
1
vote
3answers
32 views

Find position of matched word in a line in bash (not position of character)

I have a line like Xckt VDD VSS sig[1] sig[2] sig[3] sig[4] sig[5] sig[6] CKT and I want to find the position of sig[4] in the line. In this case it should return 7. I wanted some simple script/ ...
1
vote
1answer
47 views

BASH Variable checker function

I am struggling to figure out a way to reduce code duplication. Specifically when checking if a variable is set. My code currently looks like this: # webadmin enabled if [ -f "${servercfgfullpath}" ...
0
votes
2answers
94 views

Why does `read` not pick up all my vars?

My apologies for the very basic question, but can someone please help explain why the following does not print 3 2 1 6 5 4? echo '1 2 3 4 5 6' | while read a b c; do echo result: $c b a; done ...
0
votes
0answers
53 views

Executing remote code with arguments

Why this: ssh -i ~/.ssh/master.pem ec2-user@xx.xx.xx.xx 'bash -s' < `dirname $0`/elements/"$element" returns hostname.sh test-5000: No such file or directory when $element is not a just a single ...
1
vote
3answers
136 views

Do the two styles of for-loops differ in what they can express?

There are two styles of syntax of for-loop: for (( expr1 ; expr2 ; expr3 )) ; do commands ; done for name [ [in [words ...] ] ; ] do commands; done At a glance, the kinds of looping that two can ...
1
vote
1answer
69 views

Can the command within for-loop affect the iterations?

In Bash, the syntax of the C-like for-loop is: for (( expr1 ; expr2 ; expr3 )) ; do commands ; done Can the execution of commands affect the evaluations of the three arithmetic expressions ...