Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [shell-script]

Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

5
votes
3answers
7k views

How do I create a bash script that sums any number of command line arguments?

How would I create a bash script that the user can use to sum any amount of command line arguments? For example, say my script is called sum: sum 3 3 sum 3 5 8 sum 9 8 21 38 And so on.. I ...
5
votes
4answers
416 views

Renaming multiple files with rename

I need to rename files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be named. ...
5
votes
3answers
10k views

Bash script to remove the oldest file from from a folder

I want to make a bash script to delete the older file form a folder. Every time when I run the script will be deleted only one file, the older one. Can you help me with this? Thanks
3
votes
3answers
14k views

Date Difference Calculation [duplicate]

I am looking to calculate time difference between the two below Value1=2016-10-13 14:19:23 Value2=2016-10-13 18:19:23 I want to get difference in the form of hours/minutes. Any quick solution ...
3
votes
0answers
297 views

How to use `gnome-terminal` with new client/server architecture as wrapper?

Anyone knows how to accommodate for new gnome-terminal-server magic? Before gnome-terminal got its new "client/server" architecture I used to use it as vim wrapper with great success for many years. ...
2
votes
2answers
2k views

Redirect STDERR and STDOUT to different variables without temporary files

func() { echo 'hello' echo 'This is an error' >&2 } a=$(func) b=??? I'd like to redirect the stderr to b variable without creating a temporary file. echo $b # output should be: "...
2
votes
1answer
2k views

Prevent SIGINT from interrupting function call and child process(es) within

Consider the following script: #!/bin/bash set -o pipefail set -o history trapper() { func="$1" ; shift for sig ; do trap "$func $sig" "$sig" done } err_handler () { case $2 in ...
2
votes
1answer
54 views

Why does this script not accept two arguments?

Code #!/bin/sh # http://tex.stackexchange.com/a/20886/13173 # http://unix.stackexchange.com/a/280830/16920 # $Id: pdf2eps,v 0.01 2005/10/28 00:55:46 Herbert Voss Exp $ # Convert PDF to encapsulated ...
1
vote
3answers
369 views

What's the advantage of mere Ansible on mere shell-scripting if one doesn't use communally-maintained Ansible-Galaxy roles? [closed]

I establish a LAMP with a few extras on a remote Debian machine with a similar shell (Bash) script: #/bin/bash apt update -y apt upgrade ufw sshguard unattended-upgrades wget curl git zip unzip tree ...
1
vote
5answers
325 views

Separate environment with different /bin/sh

I have a bunch of shell scripts which incorrectly assume /bin/sh to be equivalent to /bin/bash. E.g., they have the #!/bin/sh shebang, but use the source command instead of . (dot). I run Ubuntu 16, ...
1
vote
2answers
3k views

Concatenate pairs of files in a loop

I have two lists of files ending with two different extensions and I would like to concatenate them in pairs, in a loop. The file names look like these: These are the files a.ID, b.ID, c.ID, d....
12
votes
0answers
422 views

Should I use $* or $@? [duplicate]

Which one of $* or $@ is better to use (in array syntax,command line parameters, etc.) to avoid any bugs/problems in code? Or it does not make any difference?
5
votes
1answer
134 views

Joined pipelines

Considering a routine such as this one: alpha() { echo a b c |tr ' ' '\n'; } which outputs a stream, I would like to take the output stream, transform it, and paste it with the original output ...
5
votes
3answers
1k views

Find files in directory with paired names

I'm running tests of a variable-size-and-contents test set. Data files are added and removed frequently. I'm looking for an automated way of gathering a file list. All files are in subdirectories of ...
5
votes
2answers
6k views

Arg list too long error while using find

I have the find command as follows: find /jobs/logs/* -prune -name '*.log' -type f -mtime +1 | while read LOGFILE do echo $LOGFILE done This is failing due to the following error:...
5
votes
3answers
21k views

Find files which are created a certain time after or before a particular file was created

I need a shell script which finds files which are created 1 hour before or 1 hour after a particular file (test.txt) was created. If I go with find -newer, that means I'd have to create a temporary ...
3
votes
1answer
992 views

How to force ssh to execute command on the remote system instead of locally

I have SSH executing some commands in a script, like so: #!/bin/bash REMOTEUSER=$1 REMOTEHOST=$2 newVh=$3 ssh "$REMOTEUSER"@"$REMOTEHOST" << EOF cd if [ ! -d "/data/web/someDirectory" ] ...
3
votes
2answers
1k views

Merging files by rows

Is it possible to copy the whole rows of File1 in a new File3 following the instruction given by File2 by using a simple bash script using sed or awk? File1: /*two or more columns*/ AC 456324 DC ...
3
votes
1answer
11k views

Bash Script getting stuck during execution

I have a bash script that is reading a CSV file , that contains source IP address , destination IP address and destination port. My script is basically doing 2 tests: 1 Performance 2 Connectivity. ...
2
votes
2answers
951 views

Directories are listed twice

I have something odd. I creates some directories using a very simple script : ... create all directories ( running under a user that has the correct rights ) e.g : mkdir -p /home/EAI003A/05_EDSNFE/...
2
votes
1answer
2k views

Trying to send multiple text attachments via mail message

Running this script on: "Red Had Enterprise Linux Server release 5.11 (Tikanga)" Mail is: version 8.1 6/6/93 (appears quite old). Have tried various things and cannot seem to get this to work.. ...
2
votes
3answers
2k views

Run script and not lose access to prompt / terminal

I am running jmeter by launching the .sh script, once I do that I cannot start using the prompt again unless I ctrl-z, ctrl-c, or the script ends in some other way. How can I launch that script and ...
2
votes
1answer
1k views

Udev: running a shellscript that accesses an X display [duplicate]

After getting my 3g-modem to work in Arch Linux I wanted to eliminate some manual labour required in the process. Hence I needed to create an udev rule. After some trial and error I came up with the ...
1
vote
1answer
929 views

Understanding an iptables shell script

SITUATION: I recently found the following shell script that works with iptables to block all internet access to/from the linux OS, except for terminals opened that were in a group called internet: ...
1
vote
4answers
562 views

Bash script Multiply only numbers from a text file?

I am looking for a bash command that will multiply only numbers from a text file. Below is the content of my text file. I need multiply all numbers by 100. 0 4530000 sil 4530000 11100000 ow ...
1
vote
3answers
1k views

Find records present in file 1 also in both file 2 and file 3

I have three files file1.txt, file2.txt, file3.txt and they are of same format. I want to select records from file1.txt which are present in both file2.txt and file3.txt based on column 2 and column ...
1
vote
2answers
31k views

How to change the working directory of invoking shell using a script? [duplicate]

Possible Duplicate: changing current working dir with a script I am trying to create few scripts that would change the working directory of the main shell/terminal. Not able to do so. I tried the ...
1
vote
2answers
852 views

Quoting / escaping / expansion issue in “command in a variable”

I want to run a command such as this in a bash script: freebcp <authentication and other parameters> -t "," -r "\r\n" When run directly on the command line, the command works as expected. But ...
-1
votes
1answer
410 views

History substitution fails when implemented in shell script

The below command works in command line rsync -avh -r /Source/09_03_2016/ /Destination/ echo $(basename !:3) Output 09_03_2016 But when I do this in shell script it does not work #! /bin/bash /...
147
votes
4answers
188k views

What is the meaning of IFS=$'\n' in bash scripting?

At the beginning of a bash shell script is the following line: IFS=$'\n' What is the meaning behind this collection of symbols?
94
votes
8answers
154k views

Passing named arguments to shell scripts

Is there any easy way to pass (receive) named parameters to a shell script? For example, my_script -p_out '/some/path' -arg_1 '5' And inside my_script.sh receive them as: # I believe this ...
89
votes
4answers
148k views

Multiline shell script comments - how does this work?

Recently, I stumbled upon a multiline comment type I have never seen before - here is a script example: echo a # : aaa : ddd # echo b This seems to work, even vim syntax-highlights it. What is ...
37
votes
3answers
9k views

Which are the standard commands available in every Linux based distribution?

I would like to know which are the standard commands available in every Linux system. For example if you get a debian/ubuntu/redhat/suse/arch/slackware etc, you will always find there commands like: ...
19
votes
6answers
48k views

generate consistent machine unique ID

Can we generate a unique id for each PC, something like uuuidgen, but it will never change unless there are hardware changes? I was thinking about merging CPUID and MACADDR and hash them to generate ...
68
votes
4answers
25k views

Fix or alternative for mktemp in OS X

I'm looking at a bash script someone else wrote that uses mktemp: TEMP=`mktemp --directory` However, this line does not work on my machine (OS X 10.6). How would I fix this line so that it is cross-...
46
votes
8answers
77k views

Bash script to get ASCII values for alphabet

How do I get the ASCII value of the alphabet? For example, 97 for a?
29
votes
8answers
19k views

How to efficiently generate large, uniformly distributed, random integers in bash?

I have been wondering what would be the best way to get good randomness in bash, i.e., what would be a procedure to get a random positive integer between MIN and MAX such that The range can be ...
71
votes
3answers
107k views

How to check if there are no parameters provided to a command?

How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?
32
votes
10answers
42k views

Can shell variable include - character?

I am not able to use - in variables in shell. Is there a way to be able to use it, because I have one script which depends on such named variables: $export a-b=c -bash: export: `a-b=c': not a valid ...
24
votes
3answers
12k views

Rule for invoking subshell in Bash?

I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process. However, this doesn't seem to be the case. In Code ...
23
votes
2answers
18k views

What's the difference between single and double equal signs (=) in shell comparisons?

Read that for comparing strings inside if we need to use double square brackets. Some books says that comparison can be done by =. But it works with the == too. #!/bin/bash a="hello" b="world" if [[ $...
27
votes
6answers
21k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func is ...
26
votes
7answers
16k views

Portable way to get script's absolute path?

What is a portable way for a (zsh) script to determine its absolute path? On Linux I use something like mypath=$(readlink -f $0) ...but this is not portable. (E.g., readlink on darwin does not ...
15
votes
2answers
112k views

what is >> symbol and >& in unix/Linux? [duplicate]

I have a CRONTAB entry as below. Can someone tell me what the below statement is exactly doing? 1 0 * * * /vol01/sites/provisioning/MNMS/45627/45627.sh1 >> /vol01/sites/provisioning/MNMS/45627/...
6
votes
4answers
7k views

Is there any book , Tutorial on very very advanced shell scripting [closed]

I am looking for some tutorial where a simple script is written using very advanced methods so that i can learn more from
6
votes
1answer
57k views

convert CSV to XLS file on linux [closed]

The following Perl script can convert CSV file to XLS file the problem is that I need to install on customer Linux machine many Perl modules in order to run this Perl script , and actually I cant ...
25
votes
5answers
39k views

Delete range of lines above pattern with sed (or awk)

I have the following code that will remove lines with the pattern banana and 2 lines after it: sed '/banana/I,+2 d' file So far, so good! But I need it to remove 2 lines before banana, but I can't ...
24
votes
5answers
20k views

Bash in Linux v.s Mac OS

I am buying an iMac. I've been working on Linux for a long time and I do a bunch of bash scripting. I was wondering if all the bash commands and features are available on Mac OS ? Anyone has any ...
13
votes
4answers
28k views

How to open terminal, split to 9 terminals and switch between them using one script?

I would like to open a terminal, split it to lets say 9 parts (3x3) and execute some bash script. But for each terminal part different script. Can this be done using perl, python or even bash? How ...
25
votes
3answers
29k views

How to “send” variable to sub-shell?

I run the following script: VAR="Test" sh -c 'echo "Hello $VAR"' But I get : # ./test.sh Hello How can I "send" the variable VAR of my script to the shell created with sh -c?