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
1answer
33 views

extract time range from log file using AWK

I am trying to extract time range from a logfile by passing shell variables in awk. This is how my log file is. 2014-11-24 12:58:59.290 1.1.1.1 etc.. 2014-11-24 13:58:59.290 2.2.2.2 etc.. ...
1
vote
1answer
48 views

How to organize files in main directory into smaller amount of files in sub-directories [duplicate]

I have a directory of 150 files that I wanted split into sub-directories of 25 files each: 1-25 into dir1, 26-50 into dir2, and so on. How can I accomplish this? dir1/fre_4_g2_c3654214.h3 ...
5
votes
5answers
5k views

How can I copy a file in a bash script and rename it while copying and place it in the same directory

How would I copy a file "file.doc" and rename it while copying to "file_copy.doc" and place it in the same directory ? And this only by calling the script and adding the file name in the argument: ...
2
votes
1answer
31 views

How can I put this string manipulation in a loop so it continues until there's nothing left to do?

I am creating a script to strip off all parts of a url other than "domain.com". I've managed to get most off, but I'm running into a problem with subfolders. Here is my code: domain=$1 ...
-1
votes
0answers
27 views

ngrep output redirection with -q option

when using ngrep in bash with -q option the '####' characters are gone and only the matches are shown but redirecting this to a text file results only in a empty file. sudo ngrep -q >> log.txt ...
0
votes
2answers
46 views

Cron Job: Redirecting shell script output to a file

Just an one line shell script below not working as a cron job but executing directly from terminal works fine: #!/bin/bash echo "Executed" >> ./crond.log 2>&1 What might be the ...
2
votes
2answers
45 views

What does the < (left chevron / triangle bracket) do? [duplicate]

I am having a difficult time wrapping my head around this. I understand that the > (right chevron) is for redirecting the STDOUT of a program to a file, as in echo 'polo' > marco.txt will ...
2
votes
1answer
28 views

How to get a list to be entered in quotes

firstly I am a total n00b with scripting, so please forgive any incorrect terminology. I have a simple script that runs a python script (used to deduplicate emails via IMAP) after a user has entered ...
2
votes
2answers
89 views

Using tar with find

Lets say we want to find a certain files , use tar on them and delete them. I tried using touch "mycompress.bz2.tar" find . \( -name "*.c" -o -name "*.sh*" \) -exec tar ujf "mycompress.bz2.tar" {} ...
1
vote
3answers
2k views

Read line from file then delete

How can I read a series of lines from a file and then remove each line? Obviously I can read like this: while read -r line; do echo $line done < myfile.txt But if I do this: while read -r ...
3
votes
4answers
2k views

How do I create a GUI application launcher for xfce4-terminal with fish but inheriting the environment variables from bash?

If I run fish from a bash prompt, it will inherit the environment variables I have set in my .bashrc and .profile files, including the important $PATH variable. So far so good. Now, I want ...
3
votes
2answers
314 views

Getting the combined count of all occurrences of a string under multiple directories?

I have a (hopefully) interesting problem that I could use some advice on. I have a system that's essentially used for storing logs. It has a directory structure like: YYYY/MM/DD/hostname/ There are ...
4
votes
2answers
160 views

Persistent Bash Prompt Prefix Linux

I want to have the FQDN as bash prefix instead of just using the hostname. So I can change root@web: ~$ to root@web.domain.tld: ~$ I already know that that is possible by using: ...
0
votes
0answers
23 views

grep, failing to find an existing file with correct path given and checked [duplicate]

I need help with grep. Below is what I've written and the If function is working, but the grep part is saying grep: ~/tmp/file.txt: No such file or directory however if I copy and paste the pathway on ...
0
votes
3answers
45 views

How can I subtract two variables in bash?

Hi Guys am getting error please help to sort out. I am new to scripting Script: #!/bin/bash value='55' size=`df -kh | grep '/vol/cdr/MCA' | awk '{print $5}'| sed 's/%//g'` if [ "$size" -gt "$value" ...
4
votes
2answers
29 views

Question about behavior of control key shortcuts

I am confused about how Ctrl-key combinations work in terminal. In bash man page, there are various combinations such as: C-e - go to end of the line C-f - go one character forward etc. But then ...
1
vote
2answers
69 views

BASH: count # of words in each line of a document

I need to identify patterns in a text file for further analysis. So the input files may contain semi-structured text as follows; file1 905:john: abc123: john@doe.com: US 920:eric: ericaA: ...
3
votes
1answer
39 views

Bash: Why is read returning a nonzero exit status while reading my file?

I'm trying to read two files line-by-line in Bash and do something to each of their lines. Here is my Bash script: #!/usr/bin/env bash die() { echo "$@" >&2 exit 1 } extract_char() { ...
1
vote
1answer
40 views

How to follow the activity of one specific file [closed]

I have currently MAAS deployed on one of the servers. It all works (most of the time). Now I have added some lines in the WOL file for MAAS (/etc/maas/templates/power/ether_wake.template). I wanted to ...
1
vote
1answer
31 views

How can I edit my bash script to account for whitespace entries? [duplicate]

I made a script to automate ffmpeg conversion from mkv > m4v > open the m4v in Subler.app to import the metadata manually, at which point I'll move the file into my iTunes library. I use iTunes ...
1
vote
1answer
31 views

Exporting a Shell Variable From the Command Line

I'm playing around with duplicity via a script that looks like this: export PASSPHRASE=SomeLongGeneratedHardToCrackKey duplicity /etc scp://FtpUserID@ftp.domain.com/etc unset PASSPHRASE Here's a ...
-2
votes
2answers
36 views

check if ip is available [duplicate]

Is it possible to check if ip is free with a script pseudo code would look something like this if [ip is available] echo "IP is available" else echo "IP is not available" fi Can ping be used ...
0
votes
0answers
35 views

escaped strings inside variable returned in single quotes

I have a number of mp3 files without correct file names. What I wish to do is to create a script that will read the artist and title from mp3 tag and rename the file according to the tag. The script ...
17
votes
3answers
3k views

Display command in xterm titlebar

My Bash Prompt is currently setting the xterm titlebar using the following sequence: PS1='\033]0;\u@\h>\w\007' Is there an easy way to display the current command in the titlebar. For example, ...
10
votes
4answers
2k views

Why does “$(( ~33 ))” produce -34?

$ echo $(( 255 )) 255 $ echo $(( 33 )) 33 $ echo $(( ~33 )) -34 $ echo $(( ~255 )) -256 $ and my kernel is: $ uname -a Linux HOSTNAME 3.2.0-40-generic-pae #64-Ubuntu SMP Mon Mar 25 21:44:41 UTC ...
0
votes
1answer
40 views

why looping over string is different when it's in a variable

I am wondering why when you iterate over a variable n="1 2 3" you get this: $ n="1 2 3"; for a in $n; do echo $a $a $a; done 1 1 1 2 2 2 3 3 3 but if you don't put the string in a variable first ...
19
votes
4answers
7k views

Why does while [ 0 ] go into infinite loop?

I see the same behaviour for below loop as the loop with while [ 1 ]. Why is that so? while [ 0 ]; do echo "hello" done
26
votes
9answers
2k views

for vs find in Bash

When looping through files there are two ways: use a for-loop: for f in *; do echo "$f" done use find: find * -prune | while read f; do echo "$f" done Assuming these two loops will ...
0
votes
3answers
3k views

The best way to add Unix Shell variables to CURL GET request

Let’s say I use curl –X GET command with the following link: “$WEBSITE_URL/ACTION_NAME/ACTION1;id=$ID?Command=TEST&id1=$ID1&id2=$ID2&id3=$ID3” As you can see, there are some $variables ...
4
votes
1answer
80 views

Executing commands in an elevated bash process by writing to the standard input of its parent script process

I have a simple bash script bash.sh that starts another bash instance using pkexec. #!/bin/bash bash -c 'pkexec bash' When executed this shows a prompt for the user to enter their password. The ...
9
votes
2answers
3k views

How to store an image file in bash variable?

After I use the following command, pngString="$(cat example.png)" echo -n "$pngString" > tmp.png I can't open the tmp.png as a PNG file. Maybe some information is lost when I use $pngString ...
2
votes
1answer
39 views

Highlight nth column of ps output

I'm trying to find the best way to highlight a specific column of some output. Specifically, I'm trying to highlight the 9th column (which is %cpu of the command: ps -eo ...
0
votes
2answers
56 views

How to determine what IP addresses are available in a hosts table?

I'm working on a script that will read from the /etc/hosts file and determine what IPs are available in the hosts file. The way the hosts file will look is as followed: 172.17.164.2 System1 ...
1
vote
0answers
40 views

What are newline rules in Bash? [closed]

I have very long commands, I want to beautify the reading of the commands using linebreaks. What are some good line-break using \ rules in Bash? I know, I should never follow the \ with a space. ...
36
votes
3answers
13k views

Are there naming conventions for variables in shell scripts?

Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...
4
votes
1answer
34 views

bash 4.3 “multiple dot” commands fails silently

I just noted this on bash 4.3; exact version number is 4.3.42(1)-release (x86-redhat-linux-gnu). $ .. $ ... $ .... $ ..... Why do the "command not found" is not prompted ? $ ... $ echo $? $ 127 ...
0
votes
1answer
55 views

Prepare update command using sed

I have a output file that contains 3 different column data- ['AARF'],SAMPLE12,2016-01-05 12:00:00-0500 ,529OFFST,2015-04-16 08:04:21-0400 "['EPROSP_IWS', ...
0
votes
1answer
348 views

Error with sed on MAC OS X invalid command u

I have an error on MAC OS X when I'm using sed, which looks like so: sed -i '' '/user_pref("mail.identity.id1.reply_on_top", 1);/ a\ user_pref("mail.identity.id1.sig_file", ...
2
votes
1answer
30 views

pdflatex/lualatex called from shutdown script can not find installed packages

At work I have set up a little backup script on my Ubuntu 14.04.1 machine. The script backs up my machine to an external hard drive. After this is done it copies a LaTeX project to my Dropbox and ...
0
votes
3answers
12k views

Regular Expression for finding double characters in Bash

I am looking for a regular expression that finds all occurences of double characters in a text, a listing, etc. on the command line (Bash). Main Question: Is there a simple way to look for sequences ...
-9
votes
1answer
196 views

replace the the first line of all .sh files in a folder

Create list of all .sh files in current folder. Check first line of each file in this list and if file starts with #!/bin/sh then replace that with #!/bin/bash. Only modify files that have #!/bin/sh, ...
3
votes
1answer
90 views

Multiple variable assignment from different parts of one command pipeline

Say I have 2 variables that I am assigning as follows: psaux=`ps aux | grep someProcessName` pscount=`ps aux | grep someProcessName | wc -l` I would like to combine the assignments in one line, so ...
0
votes
1answer
60 views

Match words in array line by line to a seperate file

Here is a sample of my input data: $9.99,Titan the Power,www.example.com,565654 $15.99,Conan The Barbarian,www.sure.com,565438 $1.99,Julia Cesar,www.bfg.com,69722 I have written this code so ...
2
votes
3answers
35 views

echo all *.tex filenames in a specific directory

I have a directory structure like this: ├── lebenslauf │   ├── lebenslauf.out │   └── lebenslauf.tex ├── thirdPage │   ├── auto │   │   ├── test.el │   │   └── thirdPage.el │   ├── missfont.log │   ...
2
votes
2answers
64 views

Why won't this bash command run when called by python?

The command $ find ~/foo/ -type f -iname "*.txt" -print0 | parallel -0 cat uses GNU Parallel to print all the .txt files under ~/foo/. I have a python script where I want to call this bash ...
0
votes
3answers
64 views

Defining bash function dynamically using eval

I'm trying to define a bash function dynamically using following code: delegate_function() { echo "output from delegate"; } eval "parent_function() { echo $(delegate_function); }" The intent is to ...
0
votes
2answers
37 views

Awk: Print last N columns, where N is passed through a variable

My input variable $dirPath contains directory path. The action is to be able to retrieve the last N values in the directory path, where N is passed through a variable $depth. For a fixed N value (say ...
0
votes
2answers
54 views

For loop runs twice,

So I am fairly new to bash scripting, but I want a bash job that will: Create a post-dated timestamp. (either as a var, or to a file) Read lines from a file named feeds.txt. Separate the number ...
16
votes
5answers
2k views

Root shell mistakenly changed. How can I change it back to a valid shell?

I mistakenly entered chsh -s /usr/bin instead of chsh -s /bin/bash and now I can't log into a root shell, how do I start a bash shell as root manually ?
1
vote
2answers
30 views

Repeating current loop iteration if some condition is met (bash)

I'm writing a bash script which takes files and directories as arguments. It will loop through its arguments and rename the current argument if some condition is met. When this happens I want also to ...