2
votes
2answers
25 views

How do I break up a file like split to stdout for piping to a command?

I have a large .sql file full of SELECT statements that contain data I want to insert into my SQL Server database. I'm looking for how I could basically take the file's contents, 100 lines at a time, ...
3
votes
2answers
103 views

How can I print out all but the last n characters in bash?

I'm working with standard input (cating two files together) and want all but the last 10 characters of the result to be passed through the next command. All I can find through searching is using sed, ...
4
votes
3answers
94 views

Dynamically trim stdout line width in Bash

Lately, I have been experimenting with the ps command, and sometimes long paths wrap to the next line (or two) and make it hard to read. I want to pipe the ps output into another program to limit the ...
0
votes
2answers
41 views

ln -s with a path relative to pwd

I'm trying to create a bunch of symbolic links, but I can't figure out why this is working ln -s /Users/niels/something/foo ~/bin/foo_link while this cd /Users/niels/something ln -s foo ...
7
votes
6answers
602 views

Is it possible to remove folder prefix from a `ls` command

I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar and I get the output: lib/mylib_1.jar lib/mylib_2.jar ... Is there any ...
3
votes
1answer
35 views

Customize bash to always include a certain parameter

When using the "ls" command, bash prints in the following format: file1 file2 file3 file4 file5 file6 file7 file8 But when given the parameter "-1", it prints: file1 file2 file3 etc... Is there ...
0
votes
2answers
71 views

How do I run a command on a TTY from a SSH session?

Does anyone know of a way that I could run a command from my ssh session and have it display on tty1? It seems like I could do that like: script command >> /dev/tty1. However, if I were to do ...
4
votes
5answers
99 views

`ls -l` of a file and all the directories leading to it?

Before I start writing a (probably easy) script, is there a command that does the equivalent of the following one: [:~/software/scripts] % ls -ld / /usr /usr/bin /usr/bin/tee drwxr-xr-x 21 root root ...
0
votes
0answers
11 views

Convert rgba color value to a Terminal supported color

Randomly I will take the following example: rgba(122, 143, 186, 0.54) How can I convert this color to the closest Terminal supported color?
5
votes
3answers
399 views

What is the difference having double quotes or not in bash [duplicate]

I have some bash scripts, one which has the following content: #!/bin/bash source $(dirname ${BASH_SOURCE[0]})/script.sh while the other one has the following content: #!/bin/bash source ...
1
vote
2answers
43 views

About escape with SSH pipe

I want to execute the awk command with long parameters,like this: ssh host "netstat -rn|awk 'NR!=1 && NF>=6 && $1!="Destination" {printf "%-15s %-20s\n", $1, $2}'|sort -f " but ...
1
vote
1answer
60 views

How to check for a folder inside a particular folder in shell script?

I am working on a shell script and python script as well. Python script is passing certain parameters to my shell script and then I am using those parameters in my shell script. Below is my shell ...
5
votes
2answers
202 views

Get the actions for a command programmatically

Let's take the git example: $ git<TAB><TAB> git git-import-dscs git-shell git-buildpackage git-import-orig git-upload-archive git-dch git-pbuilder ...
13
votes
3answers
2k views

Why isn't there any shell command to create files?

Attention please: I am not asking how to make a file from the command line! I have been using touch for making files for years without paying attention that its main purpose is something else. If ...
2
votes
2answers
77 views

Close all file descriptors in bash

Is there a way to close all the open file descriptors, without having an explicit list of them beforehand?
0
votes
2answers
34 views

command condition sometimes integer sometimes string

I want to execute this condition while [ $(cat /path_of a file/) -eq 1 ] The condition is only correct if $(cat /path_of a file/) is an integer and not a string ! How can I overcome this ...
3
votes
2answers
81 views

Passing arguments to su-provided shell

man su says: You can use the -- argument to separate su options from the arguments supplied to the shell. man bash says: -- A -- signals the end of options and disables further option ...
1
vote
2answers
10 views

Install-on-Demand Wrapper Function for Executables

How do I make the following function work correctly # Install git on demand function git() { if ! type git &> /dev/null; then sudo $APT install git; fi git $*; } by making git $* ...
3
votes
2answers
347 views

Interpret backspaces in file

Is there a way to interpret/"execute" backspaces in a file/line using common bash commands? I have a console program which prints some stuff and than erases it using backspaces and finally writes the ...
4
votes
2answers
66 views

Command substitution vs I/O Redirection?

I am wondered about differences between I/O Redirection and Command substitution? For example when I want to redirect which command output to cd command (I know that it's not a directory) then the ...
2
votes
3answers
81 views

Redirecting only stderr to a pipe

This code snippet is from Advanced Bash Scripting Guide. # Redirecting only stderr to a pipe. exec 3>&1 # Save current "value" of stdout. ls -l 2>&1 ...
0
votes
1answer
35 views

How to pass a flag from one script to a function in another script

I have a script that installs a bunch of programs (homebrew, git, etc) and I have it log to stdout all the things that its doing along the way, but I want the ability to pass a flag, such as --no-log ...
3
votes
3answers
74 views

Bash, how to let some background processes run but wait for others?

I have (yet) another wait, &, && control flow question.. Say I have a script something like this where I want to do as much work at the same time as possible: # may take some hours ...
0
votes
4answers
55 views

bash event not found trying to match and exclude parenthesis in grep

In a very long line I'll summarize with: (foo),(bar,baz(word,right),(end) I want to print only: (bar,baz(word,right To match the second parenthesis, I exclude the word that follows the ...
2
votes
1answer
137 views

Sort data in descending order of first column, for equal values, use second column in ascending order

Allow me to clarify: Assume I have some keywords with frequency of their usage: 12 Hi 7 Hash 7 C++ 9 Superuser 17 Stackoverflow 9 LaTeX 42 Life 9 Ubuntu What I want, is to sort this data ...
1
vote
1answer
97 views

/etc/profile is not read in changed user shell

I need to run custom binary when user logged in to system ,so that I have changed user's shell to /bin/binary to /etc/passwd but I have observed that /etc/profile file was not read when I logged in ...
2
votes
3answers
75 views

Count result in a find/exec statement

I have some Perl files which I have to modify through a simple sed call. Basically, I have to remove the first line of all of them. Currently, I managed to do this : find <PATH> -type f -name ...
1
vote
3answers
38 views

How to output comma-separated strings using bash brace expansion

I'd like $ echo a{b,c,d} to output a comma-separated list like ab,ac,ad instead of the usual output ab ac ad What's the easiest way to do that on the command line?
5
votes
4answers
171 views

Run a command when system is idle and when is active again

I want to run a command when the user becomes inactive (the system is idle). For example: echo "You started to be inactive." Also, when the user becomes active again (the system is not idle ...
1
vote
1answer
56 views

Not able to copy multiple files using rsync 3.0.9 version?

I am trying to copy two files using rsync from a machineB but somehow it always copies only one file but not the second file - Here is the syntax I am using - rsync -avz ...
4
votes
2answers
227 views

Merge fields in a file

I have a file with 7 columns, a GFF file having chromosomal regions.I want to collapse the rows where REGION ="exon" to only one row in the file.The row has to be collapsed on the basis of regions ...
2
votes
2answers
28 views

Setting tabstop for bash output

Is it possible to set a tabstop (number of spaces per tab) for the more and less commands? In vi, I've added this line to .vimrc set tabstop=4 However when I read through a file with more it still ...
4
votes
6answers
130 views

List all binaries from $PATH

Is there one-liner that will list all executables from $PATH in bash.
5
votes
1answer
112 views

understanding the default value of IFS

In my GNU bash version 4.2.8, IFS has a default value of space, tab and line feed by default: usr@T42 ~ $ echo -n "$IFS" | hexdump -C 00000000 20 09 0a | ..| ...
2
votes
3answers
150 views

How to use rsync or scp to efficiently copy the files from machineB and machineC to machineA?

I am running my shell script on machineA which copies the files from machineB and machineC to machineA. If the file is not there in machineB, then it should be there in machineC for sure. So I will ...
3
votes
1answer
87 views

What does exec 3<&1 do?

I understand exec can do I/O Redirection on current shell level. but I only sees usage like exec 6<&0 # Link file descriptor #6 with stdin. # Saves stdin. exec 6>&1 # ...
1
vote
2answers
78 views

How to find out all the files in other machines using bash shell script?

I am trying to find out which files are in machineB and which files are in machineC. And I need to run my shell script on machineA to figure out which files are in machineB and which files are in ...
3
votes
2answers
141 views

Set difference from two files in unix

I am looking for an efficient way to achieve set difference from 2 files. I do know that grep -vxF -f setA setB gives me B - A. This is what I want, but my files are as follows: setA: 2 4 5 6 set B: ...
1
vote
2answers
41 views

create file from function

#!/bin/bash # trims trailing spaces and tabs from file, using awk utility function remove_trail() { [ ! $# -lt 2 ] || { echo "Usage: $FUNCNAME file-to-trim out-file"; return 1; } # args ...
1
vote
4answers
52 views

Write program output to log file containing PID in its name

How can I start a program and write its output to a log file, where the log file contains the PID in its name? I tried program_a > log_$! which doesn't work since $! is the PID of the last ...
1
vote
1answer
58 views

Single command to login to SSH and run program?

Is there a way to structure a single command to login via SSH to a remote server and run a program on the remote login shell? In the OpenSSH manual, it reads "If command is specified, it is executed ...
5
votes
1answer
167 views

When is it important to write portable scripts?

Most code I write is in PHP. I have recently started learning shell scripting. Most of the resources and tutorials that I've come across are specific to Bash. Some warn about bashisms and some don't. ...
1
vote
1answer
71 views

How to exit out of the shell script successfully so that python subprocess think it is successfull exit? [closed]

Below is my shell script which simply execute a url like this http://example.com:8080/beat and parse the response and verify certain conditions on it. If that condition is met, then exit successfully ...
0
votes
4answers
172 views

Starting with bash: -lt and -gt arguments

I'm starting with bash and I found the following: if test $first -lt $second then echo $first is lower than $second else if test $first -gt $second then echo $first is higher than $second ...
5
votes
2answers
354 views

Making sure a command has executed in bash

I have a bash file with commands to merge pcap files. I want to know if there is anyway to make sure a line in bash runs or not. Like in windows bat file we have %ErrorLevel% ¨ Say a command touch -r ...
-4
votes
3answers
132 views

How to get IP Address using shell script? [duplicate]

Want to get the IP address using Shell script. Without knowing the eth0 or eth1 or eth2 How to get the particular IP address. I am not interest to get localhost address, want to get private IP ...
-4
votes
4answers
86 views

How to add a 3 lines in a file using shell script? [closed]

I have a file, that file contain 10 lines. And now I want to add 3 lines to end of the file using shell script. After add lines, If I run the script again I don't want to add same lines again. ...
7
votes
1answer
324 views

Is this redirecting to /dev/null?

I found this script on the arch wiki which prefixes an ffmpeg command like this < /dev/null ffmpeg -i "$a" -qscale:a 0 "${a[@]/%flac/mp3}" I understand the ffmpeg command, it is the < ...
2
votes
2answers
43 views

What does `env COLUMNS=%S ps …` do?

I have seen the syntax env COLUMNS=%sin shell scripts, e.g. see the line below from the Helm package in Emacs: env COLUMNS=%s ps -axo pid,user,pri,nice,ucomm,tty,start,vsz,%%cpu,%%mem,etime,command ...
1
vote
2answers
77 views

Running a bash script as root prevents asynchronous execution?

The script below get's executed differently if run as a root user. #!/bin/sh gnome-terminal -x sleep 10 echo 'Why must I wait for sleep to finish?' If its run as a regular user, message is ...