We’re rewarding the question askers & reputations are being recalculated! Read more.

Questions tagged [shell-script]

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

Filter by
Sorted by
Tagged with
4
votes
1answer
27k views

How to use variables in sed command

I'm trying to run sed -i "/$variable/c \$variable1' file.txt But it's not displaying any change in the file: file.txt. But the same command, with out the variables works fine: sed -i "/variable/c \...
4
votes
2answers
634 views

List of shells that support `local` keyword for defining local variables

I know that Bash and Zsh support local variables, but there are systems only have POSIX-compatible shells. And local is undefined in POSIX shells. So I want to ask which shells support local keyword ...
3
votes
1answer
2k views

Brace expansion not working in a script

for i in {1..40} do echo $i done I got {1..40} and I would like to have something like 1 2 3 and so on so I can use the variable i inside a command's parameter.
3
votes
4answers
2k views

How to use sed command to modify a file's entry line by line in shell script?

This is my file1: #$BQ #{ VOL @home } #database daba #relation tcdeatid #copy 1 #{ version 0 } #opendb #clear #.lruno := 72 #.infno := 1 #.tid.noel := 101 #.tid.info :=...
1
vote
2answers
796 views

How can I capture stdout/stderr in a file but still see it in my console?

I'm using bash script on AMazon Linux. When I want to redirect stderr and stdout to a file, I can run ./my_process.pl &>/tmp/out.txt Is there a way I can redirect this output to a file and ...
12
votes
2answers
87k views

./executable: cannot execute binary file

I have a script that works well when I ssh to the server to execute it myself, but has problems when Hudson, a continuous integration server, runs it. I am automating tests on an embedded linux ...
7
votes
3answers
13k 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
6
votes
3answers
4k views

tarring in parallel

An Oceanographer friend at work needs to back up many months worth of data. She is overwhelmed so I volunteered to do it. There are hundreds of directories to be backed up and we want to tar/bzip ...
6
votes
9answers
19k views

script to count files in a directory [duplicate]

I'm looking for a script that counts files in the current directory (excluding sub directories). It should iterate through each file and if not a directory increment a count. The output should just be ...
5
votes
4answers
430 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
8k 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 ...
3
votes
0answers
340 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. ...
3
votes
3answers
17k 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 ...
2
votes
1answer
61 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 ...
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 ...
1
vote
3answers
401 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
424 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, ...
12
votes
0answers
432 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?
7
votes
1answer
476 views

Variable in sed

I have a file that contains filenames with absolute path. In the middle of that path, is a variable directory, whenever I hard code the variable part, it works fine, but when I replace it with the ...
6
votes
4answers
2k views

Shell, Concatenating 2 strings to reference a 3rd variable

I am trying to do the following in my script: #/bin/bash -x var1=www var2=www2 var3=www3 var4=www4 for i in 1 2 3 4; do echo \$var$i done Obviously, this doesn't work as expected and results in ...
5
votes
1answer
142 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
2answers
7k 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 ...
5
votes
4answers
5k views

Why cat, grep and other commands can't understand files starting with minus sign? [duplicate]

If I have a file which name starting with single or several minus sign, for example --1 it can't be used as a parameter of many commands. Even if I run cat --1 instead of file content I get ...
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 ...
3
votes
1answer
1k 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
12k 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
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 ...
2
votes
2answers
974 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
2answers
3k views

Date “1 month ago” not working in AIX

In machine A (running Oracle Linux Server release 6.4), I am able to get the date of 1 month ago intelligently by using the following command: $(date -d"1 month ago" '+%Y0%m') But it is not working ...
2
votes
1answer
534 views

How to (Memory Limited) > grep -F -f file_A file_B >> output.txt

file_A (~500MB, 1.6M lines) consists of all equal length search terms, 1 per line, not sorted. file_B consists of all equal length text lines, 1 per line, not sorted I've been able to run "grep -F -...
2
votes
1answer
3k 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 ...
1
vote
1answer
1k 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
2answers
2k views

status command doesn't recognise `-p` option in Ubuntu

status -p $pidfile -l $lockfile $prog This command works on CentOS - /bin/sh shell. But when I try to run the same command on Ubuntu 14.04 with /bin/bash shell (or zsh as well), it throws error, ...
1
vote
2answers
916 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
vote
4answers
640 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
2answers
32k 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
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
1answer
11k views

Script failing with “command not found: ^M”

When I try to run the following script in zsh, via the command /bin/zsh ~/.set_color_scheme.sh I get the following error: command not found: ^M The script has u+x permissions and it used to work on ...
0
votes
2answers
1k views

No of files and directories in a particular directory in shell script

I am working on Linux and it's new to me. I created a shell script countfiledirs. I want to know the number of files and directories in a particular directory. My code is: #!/bin/bash echo "total ...
0
votes
2answers
58 views

Replace log file without affecting “Redirection of stdin and stdout” using nohup

production.log [2019-02-11 10:18:18] GET / [2019-02-11 11:18:19] POST blavadasdasdgd ... ... <--- A lot of in between logs and other data ... [2019-02-12 11:18:20] INFO ::HTTPServer#start: pid=...
189
votes
13answers
433k views

Delete the last character of a string using string manipulation in shell script

I would like to delete the last character of a string, I tried this little script : #! /bin/sh t="lkj" t=${t:-2} echo $t but it prints "lkj", what I am doing wrong?
114
votes
8answers
175k 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 ...
93
votes
4answers
161k 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 ...
98
votes
8answers
83k views

The “proper” way to test if a service is running in a script

My problem: I'm writing a bash script and in it I'd like to check if a given service is running. I know how to do this manually, with $ service [service_name] status. But (especially since the move ...
45
votes
7answers
97k views

File extensions for unix shell scripts [closed]

On wikipedia, the article for .sh says: For the .sh file extension type, see Bourne shell. How about other unix shells? I know that the shebang is used inside the file to indicate an ...
39
votes
1answer
14k views

GNU parallel vs & (I mean background) vs xargs -P

I'm confused about the difference or advantage (if any) of running a set of tasks in a .sh script using GNU parallel E.g. Ole Tange's answer: parallel ./pngout -s0 {} R{} ::: *.png rather than say ...
39
votes
3answers
10k 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: ...