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

learn more… | top users | synonyms (2)

11
votes
3answers
160 views

Understand “IFS= read -r line”?

I obviously understand that one can add value to internal field separator variable. For example: $ IFS=blah $ echo "$IFS" blah $ I also understand that read -r line will save data from stdin to ...
11
votes
7answers
6k views

Print shell arguments in reverse order

I am a bit stuck. My task is to print the arguments to my script in reverse order except the third and fourth. What I have is this code: #!/bin/bash i=$# for arg in "$@" do case $i in ...
11
votes
2answers
9k views

Regular expression in bash script

This is my first time bash scripting so I'm probably making an easy mistake. Basically, I'm trying to write a script that gets the groups of a user, and if they are in a certain group, it will log ...
11
votes
3answers
1k views

Are free shell accounts safe?

I am trying to identify how safe the free shell accounts provided by the likes of shellmix.com are. I have never used one myself but want to suggest it to someone interested in learning shell ...
11
votes
2answers
271 views

How to start tailing a file that has not been yet created

I use tail to monitor the progress of jobs that I know will write their progress to disk. Almost always, I know which file they will create before they start running (the jobs are dispatched by a ...
11
votes
1answer
4k views

SSH causes while loop to stop

I have finally managed to boil down a problem I have been struggling with for a few weeks. I use SSH with "authorized keys" to run commands remotely. All is fine except when I do it in a while loop. ...
11
votes
1answer
1k views

Suppress output unless non-zero exit code

What's the best way to suppress output (stdout and stderr) unless the program exits with a non-zero code? I'm thinking: quiet_success() { file=$(mktemp) if ! ( "$@" > "$file" 2>&1 ); ...
11
votes
2answers
167 views

Correct behavior of EXIT and ERR traps when using `set -eu`

I'm observing some weird behavior when using set -e (errexit), set -u (nounset) along with ERR and EXIT traps. They seem related, so putting them into one question seems reasonably. 1) set -u does ...
10
votes
6answers
1k views

Is it possible to substitute “then” with brackets?

I first learned how to code in a C language, and using if ( code ) then code fi makes it really hard for me to read my code (don't ask why). Is there a substitute for then? Something like this: ...
10
votes
3answers
7k views

How to sort the string which combined with string + numeric using bash script?

This is the data what I want to sort. But sort treats the numeric to string, the data it no sorted as I expected. /home/files/profile1 /home/files/profile10 /home/files/profile11 ...
10
votes
4answers
9k views

How do I handle switches in a shell script?

Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments ...
10
votes
2answers
14k views

Fastest way to concatenate files

I've got 10k+ files totaling over 20GB that I need to concatenate into one file. Is there a faster way than cat input_file* >> out ? The preferred way would be a bash command, Python is ...
10
votes
3answers
23k views

What does $# mean in shell?

What does $# mean in shell? I have code such as if [ $# -eq 0 ] then I want to understand what $# means, but Google search is very bad for searching these kinds of things.
10
votes
3answers
50k views

How to rename multiple files in single command or script in Unix? [duplicate]

I have the below list of files aro_tty-mIF-45875564pmo_opt aro_tty-mIF-45875664pmo_opt aro_tty-mIF-45875964pmo_opt aro_tty-mIF-45875514pmo_opt aro_tty-mIF-45875524pmo_opt that I need to rename to ...
10
votes
3answers
409 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'
10
votes
1answer
861 views

How do you omit “./” when running scripts on current directory

On some tutorials over the internet they can run a script on the current directory without doing: ./script How do you omit this? I am using CentOS 5.6 x64. I want to run it this way script
10
votes
6answers
2k views

How to extract a key value pair from ps command

I am trying to extract application name from ps command: ps -af -u sas |grep "java"| grep -v "grep" above command produces the following: sas 24431 1 0 Oct10 ? 00:51:08 ...
10
votes
4answers
13k views

Bash script to detect the version control system by testing command return status

I am working on a bash script that I would like to work for several types of VCS. I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the ...
10
votes
2answers
484 views

Having a problem with a shell script counting characters

I'm trying to learn the basics and I have run into an issue with my script counting the characters of a user's input. Here is my script, can someone point out where I'm going wrong please? ...
10
votes
6answers
18k 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 ...
10
votes
4answers
33k views

Appending current date to filename

I'm trying to append the current date to the end of a file name like this: TheFile.log.2012-02-11 Here is what I have so far: set today = 'date +%Y' mkdir -p The_Logs & find . -name The_Logs ...
10
votes
2answers
2k views

Piping for loop output prevents local variable modification

I'm trying to write a simple bash function that takes, as its arguments, a number of files and/or directories. It should: Fully qualify the filenames. Sort them. Remove duplicates. Print all that ...
10
votes
3answers
740 views

What's a smart way to count the number of days since X?

I started working at my current position since November 17th 2014. I would like to know how many days have run up to now. Any ideas on how to use Linux to come up with a simple and nice solution ?
10
votes
3answers
1k views

What does `1>>` and `2>>` mean in a bash script?

I have the following bash script, from what I understand >> is used to append the output of a command to an existing file instead of overwrite, but what is it doing in this case? This script is ...
10
votes
5answers
30k views

Add lines to the beginning and end of the huge file

I have the scenario where lines to be added on begining and end of the huge files. I have tried as shown below. for the first line: sed -i '1i\'"$FirstLine" $Filename for the last line: sed -i ...
10
votes
4answers
837 views

What is the easiest way to execute text from tail at the command line?

Sometimes I'm working on a new (ubuntu) box and I type git and am alerted: The program 'git' is currently not installed. You can install it by typing: apt-get install git-core If that happens I ...
10
votes
1answer
2k views

How do I make a shell script that sends output to a process

I'm currently running a server console program in a screen because I need to both read it and occasionally send commands. I'd like to run the app as a deamon in the background (start/stop it with ...
10
votes
4answers
10k views

find -exec in bash script with variable expansion

I'm trying to run a command similar to the one below in a bash script. It should search through all subfolders of $sourcedir and copy all files of a certain type to the root level of $targetdir. ...
9
votes
8answers
3k views

Is there a unix command that gives the maximum of two numbers?

I was looking for a command to limit numbers read in from stdin. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, ...
9
votes
4answers
735 views

How can measure how long it takes my script to run AND include that in an email it generates?

I have a simple bash script that runs a series of checks (ping, nslookup, etc) and then sends an email report with the output of that data. I'd like the email to include information on how long it ...
9
votes
5answers
2k views

Get total duration of video files in a directory

I have a list of .ts files: out1.ts ... out749.ts out8159.ts out8818.ts How can I get the total duration (running time) of all these files?
9
votes
5answers
6k 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 ...
9
votes
4answers
16k views

Difference between ls -l and ll?

I'm relatively new to programming as a whole and some tutorials have been telling me to use ls -l to look at files in a directory and others have been saying ll. I know that ls is a short list, but ...
9
votes
4answers
5k views

How to run parallel processes and combine outputs when both finished

I have a bash shell script in which I pipe some data through about 5 or 6 different programs then the final results into a tab delimited file. I then do the same again for a separate similar dataset ...
9
votes
3answers
2k views

Substituting strings in a very large file

I have a very long series of urls with no separating character, in the same format as below: http://example.comhttp://example.nethttp://example.orghttp://etc... I want each URL to be on a new line. ...
9
votes
2answers
212 views

How does the syntax “${foo##*.}” work to get file extensions? [duplicate]

Why does this command work in successfully retrieving file extension name? file_ext=${filename##*.}
9
votes
2answers
10k views

Bash scripting - loop until return value is 0

I need to umount something in my script, but sometimes it unmount's before all of the data has finished being copied and causes the umount to fail. I looked for a way to do a "blocking" umount, but I ...
9
votes
4answers
24k views

How do I append text to the beginning and end of multiple text files in Bash?

I have a directory full of text files. My goal is to append text to the beginning and end of all of them. The text that goes at the beginning and end is the same for each file. Based on code I got ...
9
votes
4answers
3k views

How to prepend a license header recursively for all .h and .cpp files in a directory

I'm trying to add a license header to all header files and source files in a project directory using a for loop. This is not working, is there any other approach using sed?
9
votes
2answers
860 views

date - years prior to 1901 are treated as invalid

I have date (GNU coreutils) 7.1 installed on my system. If I try to check dates prior to 14-Dec-1901, I get an "invalid date" error. For example, $ date -d 1901-12-13 date: invalid date ...
9
votes
3answers
37k views

How can I concatenate a shell variable to other other parameters in my command lines?

How can I concatenate a shell variable to other other parameters in my command lines ? For example, #!/bin/sh WEBSITE="danydiop" /usr/bin/mysqldump --opt -u root --ppassword $WEBSITE > ...
9
votes
2answers
4k views

Do parentheses really put the command in a subshell?

From what I've read, putting a command in parentheses should run it in a subshell, similar to running a script. If this is true, how does it see the variable x if x isn't exported? x=1 Running ...
9
votes
6answers
11k views

Shell script for moving oldest files?

How do I write a script for moving just the 20 oldest files from one folder to another? Is there a way to grab the oldest files in a folder?
9
votes
2answers
2k views

How can I delete all files in a folder that haven't been accessed in a certain amount of time?

I'd like to run a nightly cron job that deletes all the files in a folder that haven't been accessed in a week or more. What is the most efficient way to do this in bash?
9
votes
7answers
977 views

How to merge text of alphabetic lines with the numeric lines in shell?

I Have a file that has text like this: AAAA BBBB CCCC DDDD 1234 5678 9012 3456 EEEE 7890 etc... And i want to match up the Alphabetic lines with the Numeric lines so they are like this: ...
9
votes
2answers
4k 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 (( ...
9
votes
5answers
4k views

Exit Google Chrome from terminal

Is there a way to cause google-chrome to quit, from the terminal, besides using killall google-chrome? I would like to be able to close it from a script without killing it.
9
votes
1answer
826 views

communication between multiple processes

I have a bash script, which runs manager() function as a separate process for x-times. How is it possible to forward messages to all manager() processes from within the script? I've read about ...
9
votes
5answers
16k views

Binary to hexadecimal and decimal in a shell script

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?
9
votes
4answers
358 views

Unexpected EOF and syntax error

I am currently writing my third ever shell script and I have run into a problem. This is my script so far: #!/bin/bash echo "choose one of the following options : \ 1) display all current users \ ...