Tagged Questions
Questions about bash only, as opposed to other Bourne/POSIX shells. For all Bourne/POSIX-style shells, use the /shell tag instead.
2
votes
2answers
25 views
Get the last minute dated lines from list
If I have an input file with a list of users and their dates they were last signed in, how can I select with a sort | awk function the last minute lines?
The dates are written in a +%H:%M:%S format.
...
0
votes
1answer
10 views
trapping signals inside grouping commands
I recently ran into a strange behavior. Trap doesn't get executed if inside grouping command:
$ cat ./1.sh
#!/usr/bin/env bash
set -eu
trap 'echo exit' EXIT
$ ./1.sh
exit
$ cat ./2.sh
#!/usr/bin/env ...
2
votes
0answers
25 views
how to write script that should not exit the terminal when I press Ctrl+C
I have a script which is running some of (more than 15) applications like:
cd mypath
sleep 1
gnome-terminal -t App1 --tab -e "./App1"
sleep 1
gnome-terminal -t App2 --tab -e "./App2"
..
..
sleep ...
0
votes
2answers
29 views
Create a log file with the same name as input file
I'm new to linux shell scripting. I have few input files that I have to parse one by one. So in my "work in progress" WIP folder I have one input file at a time. While one file is under processing I ...
0
votes
1answer
32 views
Bash if conditions of variables within intervals
I have some data spread over time intervals, and I want to take some of those data within the time intervals. For example, I have data at times within 1..9, 11..19, etc., and I want to take data ...
0
votes
0answers
17 views
Parameters files, pfiles director: how to overwrite default settings
This is a quite general question that I am not sure to be able to address correctly.
I must manage some bash or tcsh scripts that invoke other software's tools.
These tools are invoked by name and ...
2
votes
2answers
35 views
awk: Blank line with date
I use the following code to write the online users' process numbers and the date they were checked:
w | tail -n +3 | awk '
{ "date +%H:%M:%S" | getline tim}
{if ($1 != "")
...
1
vote
5answers
65 views
How to copy a file from a remote network to the local desktop
So I used SSH to remotely login to another computer on a network. I have many files that I would like to copy over to my local desktop.
I am trying to follow this How-to :
However, I think one ...
3
votes
4answers
157 views
Removing duplicate lines
Say, I have two commands that return some text. For example:
$ ./c1
/usr/bin/foo
/usr/bin/bar
/usr/bin/baz
$ ./c2
/usr/bin/foo
/usr/bin/qux
/usr/bin/buzz
/usr/bin/bar
I want to remove the duplicate ...
9
votes
4answers
249 views
Recursively iterate through files in a directory
Recursively iterating through files in a directory can easily be done by:
find . -type f -exec bar {} \;
However, the above does not work for more complex things, where a lot of conditional ...
0
votes
1answer
37 views
Copy stdout contents to a file in command line mode
I have a program like this. I am in terminal mode. I want to copy the outptu contents to another file. First I tried redirecting, but it didnt work due to buffering. Then i tried unbuffer command. But ...
1
vote
0answers
36 views
Integer part from float operations without | bc
I read a billion of discussion about this but still have confused thoughts.
I just need to use bash to calculate some parameters, and for this operation I need to get the integer part of a float ...
2
votes
1answer
44 views
keep aliases when I use sudo bash
I have a Centos 5.5 server that has my own alias commands for my account. When I do "sudo bash" I can still use those aliases, I didn't have to add the aliases to the root bashrc.
I have another ...
2
votes
2answers
15 views
Clone repos with a similar name
I would like to do something like:
git clone https://github.com/meteor{A,B,C,D}test
but bash does not convert the {} into an each. What am I doing wrong?
2
votes
3answers
34 views
Reuse similar flags for multiple commands
Let's say I want to execute several commands with similar flags such as:
du -sh / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4
tar cvf archive / --exclude=dir1 --exclude=dir2 ...
8
votes
2answers
303 views
disable CTRL-D window close in terminator (terminal emulator)
I am often logged in through multiple SSH sessions. To logout from my sessions, I press CTRL+d, until I am back on my local machine.
However, sometimes I press once too many, and my terminal exits.
...
0
votes
1answer
23 views
How to make xargs ping and head output as expected?
Given an array of domains I would like to obtain their IP addresses, in the following format:
98.139.183.24
98.137.236.24
This script
#!/bin/bash
domains=(yahoo.com yahoo.ca)
printf "%s\0" ...
7
votes
6answers
666 views
How to read first and last line from cat output?
I have text file. Task - get first and last line from file after
$ cat file | grep -E "1|2|3|4" | commandtoprint
$ cat file
1
2
3
4
5
Need this without cat output (only 1 and 5).
~$ cat file | ...
0
votes
2answers
34 views
Output multiple strings out of multiple files
Hi my current code is:
find /home/user/logfilesError/ -maxdepth 1 -type f -name "gBatch_*"\
-daystart -mtime -1 -exec grep -rl "ERROR" "{}" + | xargs -l basename
if [ $? -eq 0 ]; then
...
0
votes
2answers
71 views
set variable options with dot
I'm trying to interface with a system not managed by me that is doing some Unix command scripts and command to easily execute a job.
There is a variable substitution that doesn't work and I wonder if ...
4
votes
2answers
150 views
if statement in .bashrc not working correctly
I set up .bashrc to show a long prompt if the terminal is at least 80 characters wide. Otherwise it shows a degraded, smaller prompt.
I use an if statement to achieve this:
dp_smaller_than=80
if [ ...
0
votes
0answers
17 views
[Solved]man crashes on OS X after upgrading bash and its manpages
Bash on OS X is always so old that I finally decided to compile a never(4.3.18) one and simply override and replace the old one.
Here are the configure flags I used:
./configure --prefix=/ ...
2
votes
2answers
109 views
How to parse multiple string patterns in linux shell script
I am new to linux shell and I had a hard time thinking of the solution to this. Can somebody help how to parse multiple strings (data) in a file? For below file, I want to parse all the values next to ...
6
votes
3answers
61 views
Print a multiline variable in a column
I have a script that collects values from a cluster. The values, in some cases, have multiple lines. I have a printf format that specifies how the data should be positioned however, it doesn't take ...
0
votes
1answer
16 views
GNU Screen: how to start a bash process and execute a command within it?
I'm trying to set up a screenrc file that opens a few windows, and in one of them I'd like to start a shell and start vim within it (rather than starting vim directly). I've tried things along the ...
2
votes
4answers
85 views
How to initialize an array using awk and bash?
I am trying to store values of the first line of a text file into an array. Here is what I have so far:
arr_values=()
awk '
NR==1 {
for (i=0; i<=NF; i++)
...
1
vote
3answers
30 views
Inserting the command line text before output in stdout (for pipe / redirection)
Consider the following bash constructs:
ls /usr/include/asm > list-redir.txt
ls /usr/include/asm | tee list-tee.txt
In this case, list-redir.txt and list-tee.txt will be identical, and will ...
5
votes
3answers
303 views
Is there any harm in using variables that aren't set?
Say I have the following code:
# Check if the color prompt is enabled and supported on this system
if [ -n "$force_color_prompt" ] && [ -x /usr/bin/tput ] && tput setaf 1 ...
2
votes
4answers
224 views
Inserting a line in a file only if this line isn't yet part the file
Total newbie to shell scripting, I am searching to create a script to automatically apply some custom configuration to text based configuration files.
In the present case, I am searching to add 2 ...
0
votes
2answers
48 views
Exit the bash function, not the terminal
I have a bash script as below which installs zookeeper but only if not installed already.
##zookeper
installZook(){
ZOOK_VERSION="3.4.5"
ZOOK_TOOL="zookeeper-${ZOOK_VERSION}"
...
5
votes
3answers
485 views
How can I view a reference or cheat sheet of the basic bash syntax, in the linux terminal?
Every time I need to do a little bash scripting involving if statements, checking for empty variables and non-existent files et.c. I find myself hitting google for some simple bash manual or guide.
...
7
votes
2answers
194 views
Cannot sum numbers received from stdin using bc
I'm trying to calculate the average entropy of files contained in a folder using:
{ echo '('; find . -type f -exec entropy {} \; | grep -Eo '[0-9.]+$' | sed -r 's/$/+/g'; echo '0)/'; find . -type f | ...
2
votes
3answers
44 views
How to set variables from find+exec?
I am trying to write a bash script where I am trying to find if some location has empty files or not and send an email if found. I first thought about combining "find" and "mail" together, but if the ...
1
vote
1answer
25 views
SSL no certificate
I'm trying to make a bash script who will check the SSL status of my web site client and return the error. But for every IP, wget https://$ip:$port gives me the following error:
ERROR: The ...
1
vote
2answers
30 views
How to guarantee availability of $BASH_ENV
A non-interactive, non-login shell will try to source any script specified in $BASH_ENV. But how do I guarantee $BASH_ENV is set before a cron job or script has a chance to set $BASH_ENV for any ...
5
votes
1answer
58 views
Why does `bash -c somecommand` sometimes not leave a bash process?
On an Ubuntu 12.04, with GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu),
I tried the following command:
$ bash -c 'pstree -s $$'
init───sshd───sshd───sshd───bash───pstree
$ bash -c 'pstree ...
1
vote
2answers
115 views
Setting PATH vs. exporting PATH in ~/.bash_profile [duplicate]
What's the difference and which is better to use when customizing my bash profile? Documentation on the export command is scarce, as it's a builtin cmd.
Excerpt from version 1 of my ~/.bash_profile:
...
-3
votes
0answers
39 views
( tr -d '' < /a.txt > /b.txt ) can do exactly what (cat /a.txt > /b.txt ) can. Why should tr version be avoided? [closed]
The only known reason would be, because tr is not intending to do what the cat command is doing in this example:
tr -d '' < /a.txt > /b.txt
cat /a.txt > /b.txt
but what if cat is a very ...
1
vote
1answer
21 views
Why isn't this XMLStarlet Query Working?
I'm trying to write a simple bash script that parses price info from the ebay developer API search results. Here's an example of XML search results for "Detective Comics 700":
...
6
votes
2answers
309 views
Do parentheses really put the command in a subshell?
From what I've read, putting a command in parentheses should run it in a subshell, similar to running a script. If this is true, how does it see the variable x if x isn't exported?
x=1
Running ...
0
votes
1answer
30 views
eval used with piped command
I have file.txt with command stored in one line (this command is valid when running in console) and I want to execute it in one line with sh like
cat file.txt | eval
what is missing? any protips?
...
0
votes
2answers
26 views
Passing a variable to a bash script that uses 'EOF' and considers the variable a literal [closed]
in this script i end up with "$1" being saved to the /test file.
#!/bin/bash
cat > /test << 'EOF'
$1
EOF
the truth is.. i need to keep
'EOF'
as 'EOF' because my argument($1) contains ...
0
votes
1answer
46 views
Bash Script : Passing a variable to a bash script that contains quotes, single quotes. etc [closed]
lets assume this is the string:
'a',"b"
it contains both single and double quotes.
how would you pass this to a bash script as a single string ?
this is the bash script:
#!/bin/bash
echo $1
...
0
votes
2answers
25 views
Write log for a false statement of an if condition which returns exit 2
I have a script running every 5 mins with two exits in a condition clause.
#!/bin/bash
date=$(date +%Y)
if [ $date -eq '2014' ]
then
echo "Current year is $date"
exit 0
else
echo "Current ...
1
vote
2answers
43 views
alias for a command using !! to recall the last command [duplicate]
This is a follow-up question to
Execute a line of commands with one sudo
If you want to redo the same command with sudo !! after doing a command like this:
echo "something">/path/file
You ...
8
votes
3answers
402 views
Execute a line of commands with one sudo
For example if I want to create a file and enter text in one line I can redirect the output into a file with the use of the > operator:
echo "something" > /path/foobar
but if I don't have ...
0
votes
3answers
48 views
Checking if line in file exist
I have a bash script which asks a user for the number of CPU cores and saves it to variable named $cores.
Now I want to add this variable to .bashrc, so I ask user how much CPU cores he has and then ...
0
votes
4answers
59 views
Error: integer expression expected
I have written a script to determine the Load average on the server as follows:
#!/bin/bash
loadavg=`top -b -n1 | grep -i load | awk -F, '{print$4}'| awk -F: '{print $2}'`
if [ "${loadavg}" -le 1 ...
1
vote
0answers
23 views
How to combine Bash's read with HERE-document when shopt -os errexit is in place? [duplicate]
I am trying to make use of the solution presented in one of the answers of http://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash
In Bash version ...
0
votes
3answers
45 views
multiplication of every line
I would like to calculate a value on every line of a file such as this:
1 1441792,11534336
2 1048576,8388608
3 1441792,1153433
4 1966080,15728640
5 393216,3145728
6 2621440,20971520
7 ...