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
3
votes
3answers
10k views

Scripting `rm` and `rm -i`

I want to create a script that whenever there are more than three files being removed, it invokes rm -i by default. I know it will go in initialization files but can't seem to get it working. So far: ...
3
votes
1answer
1k views

Open any kind of application with BASH

I have made a Java program that could open any application. Suppose there is a file name "*.jpg", the it would allow the OS to recognize the type of application and then open the default application. ...
2
votes
3answers
10k views

Using a reference to a bash string variable in sed

I'm a relative Linux novice. Suppose that I have a text file a.txt that contains the following text: A B C Now, if I want to change the text on line 2 (which contains B), I can use the following ...
1
vote
0answers
808 views

error in count script for number of occurences

I have written a small script to count number of occurrences of logs in a particular file. The following is my code: #!/bin/sh count=1 while read line do found=$( grep M $line ) OCCURENCE=$( ...
1
vote
2answers
7k views

Check if process is running Mac OS X then execute code

I am creating a script which clears the cache for Google Chrome. However, I would like to check if Chrome is open and if so not run the code but if it isn't then it will execute the code. I can see ...
0
votes
2answers
852 views

breaking of output into new lines

I created a script ./abc. I want to break the ouput into new lines. mycode: #!/bin/bash dir="$1" echo -n "size of file:" du -hba $dir | sort -n -r | tail -1 my output is: size of files: 20kb /...
222
votes
3answers
381k views

Empty the contents of a file

I am aware of three methods to delete all entries from a file. They are >filename touch filename filename < /dev/null Of these three I abuse >filename the most as that requires the least ...
165
votes
4answers
211k 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?
88
votes
13answers
251k views

How to compare two dates in a shell?

How can two dates be compared in a shell? Here is an example of how I would like to use this, though it does not work as-is: todate=2013-07-18 cond=2013-07-15 if [ $todate -ge $cond ]; then ...
65
votes
17answers
156k views

gpg asks for password even with --passphrase

I expect the following command to extract the gpg file without asking for password: gpg --passphrase 1234 file.gpg But it asks for the password. Why? This also have the same behavior: gpg --...
16
votes
4answers
34k views

How to replace spaces in all file names with underscore in Linux using shell script?

I tried following shell script which should replace spaces from all xml filenames for xml_file in $(find $1 -name "* .xml" -type f); do echo "removing spaces from XML file:" $xml_file mv "$xml_file"...
22
votes
11answers
69k views

How to compare to floating point number in a shell script

I want to compare two floating point numbers in a shell script. The following code is not working: #!/bin/bash min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min
30
votes
6answers
46k views

Trap, ERR, and echoing the error line

I'm trying to create some error reporting using a Trap to call a function on all errors: Trap "_func" ERR Is it possible to get what line the ERR signal was sent from? The shell is bash. If I do ...
22
votes
8answers
80k views

Hiding Password in Shell Scripts

How can I hide a password in shell scripts? There are a number of scripts that are accessing database. If we open the script others also aware the username and password. So if anyone knows how to hide ...
15
votes
1answer
6k views

Can an interactive shell become non-interactive or vice versa?

Can an interactive shell become non-interactive or vice versa? Note: I've done a lot of research on the basic question, "What is the difference between interactive and non-interactive?", and the ...
58
votes
3answers
31k views

Use & (ampersand) in single line bash loop

I have been using this command successfully, which changes a variable in a config file then executes a Python script within a loop: for((i=114;i<=255;i+=1)); do echo $i > numbers.txt;python ...
49
votes
2answers
95k views

suppress stderr messages in a bash script

Consider the following (slightly silly) script name 'test1.sh': #/bin/bash # sleep 10 & echo sleep pid = $! pkill sleep When I run it, I get not only the output of the echo, but bash's reporting ...
19
votes
6answers
38k views

How to move the first x files

I have this huge folder with thousands of unordered files. Is it feasible to move the first 5000s to a subfolder via the mv command? For now I move files with mv *some_pattern* ./subfolder1/ As ...
42
votes
1answer
112k views

Bash if statement [: missing `]' error [duplicate]

I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string. This works perfectly. echo $(ipcs | grep Shared | awk '{print $2}') When I put it in ...
30
votes
4answers
19k views

Is there a JavaScript shell?

Recently I've been working with JS and I'm very enthusiastic about this language. I know that there is node.js for running JS at server side, but is there a shell that uses JS as a scripting language? ...
24
votes
3answers
74k views

In a bash shell script, writing a for loop that iterates over string values

In bash, I know that it is possible to write a for loop in which some loop control variable i iterates over specified integers. For example, I can write a bash shell script that prints the integers ...
12
votes
3answers
35k views

How to launch shell script with double click in CentOS 7

I need to be able to execute an executable shell script (sh) with a double click. I set the executable flag on the permissions for the file, and yet when I double click on it, it opens in a text ...
9
votes
5answers
17k views

Compare two files with first column and remove duplicate row from 2nd file in shell script

I will ask my question with an example. I have 2 files: File #1: 118D FC300_R5_TP FX.B 32775 2112 6 2038 6 2112 0 118E FC300_R5_TP FX.B 32775 2136 7 ...
16
votes
4answers
23k views

How can I list all user names and/or home directories?

I want to list all the users dirs on the machine.Usually, I will do: ls -l /home But I use it in a script that will be deployed on others machines and maybe on that machines they don't call it home (...
15
votes
6answers
5k views

Using “reserved” codes for exit status of shell scripts

I recently came across this list of Exit Codes With Special Meanings from the Advanced Bash-Scripting Guide. They refer to these codes as being reserved and recommend that: According to the above ...
9
votes
5answers
39k views

How can I either encrypt or render my shell script unreadable?

How can I encrypt or scramble my shell script so that it's unreadable to the naked eye? Either method would be acceptable, please provide specific steps.
21
votes
5answers
4k views

Differentiating between running and being sourced in a bash shell script?

Either what I'm asking here is extremely unorthodox/unconventional/risky, or my Google-fu skills just aren't up to snuff... In a bash shell script, is there any easy of way of telling if it is ...
21
votes
2answers
1k views

Is there a POSIX way of setting zeroth argument of a target application?

In bash you can use exec -a and in zsh you can alternatively also set ARGV0 to execute a program with a certain zeroth argument but is there also a POSIX way of doing so? As suggested in this one ...
13
votes
4answers
4k views

call function declared below

Is it possible to call a function which is declared below in bash? Example if [ "$input" = "yes" ]; then YES_FUNCTION elif [ "$input" = "no" ]; then NO_FUNCTION else exit 0; fi ...
18
votes
4answers
6k views

Is it a sane approach to “back up” the $IFS variable?

I am always really hesitant to mess around with $IFS because it's clobbering a global. But often it makes loading strings into a bash array nice and concise, and for bash scripting, conciseness is ...
17
votes
4answers
5k views

How to stop a script from running if it's not root (and echo “Not running as root! Exiting…”)

Here's my source: #!/bin/bash echo "Running script to free general cached memory!" echo ""; echo "Script must be run as root!"; echo ""; echo "Clearing swap!"; swapoff -a && swapon -a; echo "...
14
votes
5answers
2k views

Get the display width of a string of characters

What would be the closest to a portable way to get the display width (on a terminal at least (one that displays characters in the current locale with the correct width)) of a string of characters from ...
13
votes
4answers
5k views

How to invoke a shell built-in explicitly?

I want to customize the functionality of cd command as per my needs. I defined the following function - function cd () { cd "$@" && pushd "$@"; } The intent of this function is to ...
12
votes
7answers
21k views

How can I safely get the version of ksh?

How can I safely get the version of ksh from within a ksh script? I have seen the following solutions: ksh --version echo ${.sh.version} echo $KSH_VERSION And given the right circumstances, each of ...
9
votes
3answers
13k views

How to compare a program's version in a shell script?

Suppose I want to compare gcc version to see whether the system has the minimum version installed or not. To check the gcc version, I executed the following gcc --version | head -n1 | cut -d" " -f4 ...
9
votes
2answers
6k views

Parallelizing a for loop

I want to parallelize the for loops of the following code. How to do this? #!/bin/bash N=$1 n=$2 for (( i=1; i<=$N; i++ )); do min=100000000000000 //set min to some garbage value for (( j=1;...
8
votes
2answers
6k views

When sh is a symlink to bash or dash, bash limits itself to POSIX-compliance, so it should be 100% compatible with sh?

From Difference between bash and sh: ABck to the question: If you have /bin/sh as a link to bash, then bash will not behave the same when called as /bin/sh as it does when called as /bin/bash. When ...
5
votes
2answers
27k views

Email using shell script

Is there a possibility to add multiple e-mails to a single variable so that when the shell script is run, it sends a mail to all the specified emails?
4
votes
2answers
3k 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: "...
10
votes
4answers
32k views

add space 5 times at the beginning of each line in a text file [duplicate]

I am looking for a command line or bash script that would add space 5 times before the beginning of each line in a file. for example: abc after adding spaces 5 times abc
9
votes
6answers
8k views

Tool to create text files from a template

I have to regularly create 100+ text files from templates. I currently use an overly complicated shell script. I think there is a smarter way to handle this, but I don't know how. I have a "database" ...
6
votes
3answers
6k views

How to return the currently active user/session on a graphical Linux desktop session? [closed]

My question is how to get the user name on the shell, who is currently using the Linux desktop (on a "normal" desktop system, where you usually only have one active user, i.e. no server system here, ...
5
votes
3answers
4k views

How to determine callee function name in a script

To make it short, doing something like: -bash$ function tt { echo $0; } -bash$ tt $0 will return -bash, but how to get the function name called, i.e. tt in this example instead?
20
votes
2answers
56k views

Files greater than 1 GB and older than 6 months

I want to find files which are greater than 1 GB and older than 6 months in entire server. How to write a command for this?
14
votes
3answers
7k views

Date validation in shell

I want to validate the below date format in shell script 2015-Jul-13 I'm using date -d "2015-Jul-13" +"%Y-%b-%d" but its giving error as date: invalid date '2015-Jul-13'
9
votes
5answers
5k views

Universal non-bash `time` benchmark alternative? [closed]

For comparing run times of scripts between different shells, some SE answers suggest using bash's built-in time command, like so: time bash -c 'foo.sh' time dash -c 'foo.sh' ...etc, for every shell ...
7
votes
4answers
8k views

Parallel execution of a program on multiple files

I have a small script that loops through all files of a folder and executes a (usually long lasting) command. Basically it's for file in ./folder/*; do ./bin/myProgram $file > ./done/$file ...
7
votes
1answer
19k views

How to assign result of sed to variable

Below is a complete copy of a demo I'm using to to figure out to get the sed command to get rid of the spaces in a persons name, and compress it down to not have spaces. Once this is done, I want to ...
7
votes
3answers
5k views

Keep running a script via ssh

ssh can use to run remote commands. ssh me@server.com 'long-script.sh' I run a long script that will take a lot of time, but I want to close my computer and keep running the script in the remote ...
5
votes
5answers
3k views

Rename files based on their parent directory using find/xargs command?

I have a directory structure like this: Project/ | +--Part1/ | | | +--audio.mp3 | +--Part2/ | | | +--audio.mp3 | +--Part3/ | | | +--audio.mp3 ... I want to ...