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

learn more… | top users | synonyms (2)

1
vote
3answers
559 views

Script to count files matching a pattern in subdirectories

I wrote the following script for finding the number of pdf and tex files from the current directory, including the subdirectories and hidden files. The following code is able to find the number of pdf ...
2
votes
2answers
269 views

Why does “$i | sed” not work?

I have this code: if [ $i | sed -e "s/^.*\(.\)$/\1/" = "/" ] then echo "folder" else echo "file" fi where $i is something like this app4/ The code above should get the last char of the ...
-2
votes
1answer
267 views

Why does this script show all files in the directory and not just PDF files?

I wrote the script below to find the number of PDF files in a given directory. However, it instead shows all the files in the directory: #!bin/bash message="." message1="*.pdf" ls -al $message ...
3
votes
1answer
560 views

wait for autossh connection to complete

I'm trying to create a script that runs a few commands that take a long time to execute and require a constant connection, but autossh user@server wait $! #or wait ${!} commandA commandB doesn't ...
11
votes
3answers
4k views

How can I receive top-like CPU statistics from the shell?

I'm trying to get an accurate read of my used CPU (in percent) from top. This is the command I'm running for testing: top -n1 | awk '/Cpu\(s\):/ {print $2}' This returns: 10.7%us, Which is the ...
11
votes
5answers
8k views

#!/bin/bash - no such file or directory

I've created a bash script but when I try to execute it, I get #!/bin/bash no such file or directory I need to run the command: bash script.sh for it to work. How can I fix this?
2
votes
1answer
2k views

Script to SCP files works manually but not through cron

I'm using CentOS 5.6. The below code might have some typos as I needed to change real info to fake info for security reasons: backup.sh #!/bin/bash set -vx rm -v /server/temp_db.gz rm -v ...
1
vote
2answers
2k views

Using rsync in a cronjob when a password is needed

Basically I have a bash script that fetches data from my server to perform a backup. As it is now I have to start that script manually, enter the password, and then wait for it to finish. I would ...
1
vote
1answer
457 views

Reading two files into an IFS while loop — Is there a way to get a zero diff result in this case?

I have a text file full of several hundred lines of sequences like this: b 29. b 52. c 84. c 83. c 94. c 93. c 61. b 38. c 81. c 92. c 28. c 37. ...
0
votes
1answer
905 views

Shell script to merge properties file (sed/awk/comm/diff) for rpm upgrade

We're developing a java product that's installed via rpm. We have a set of config files that a user can edit. These config files are java properties files in the form: com.acme.pkg1.setting=something ...
4
votes
2answers
2k views

Set alignment of numeric columns when columnating data

I use column -t to format data for easy viewing in the shell, but there seems to be no option to specify column alignment (e.g. align to the right). Any Bash one-liners to do it? I have arbitrary ...
9
votes
4answers
15k views

How to read from two input files using while loop

I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB. FileA: [jaypal:~/Temp] cat ...
3
votes
2answers
9k views

Wait for process to finish before going to the next line in shell script

I have a script I made to create a backup. I need to make sure the backup is ready before it runs the /home/ftp.sh command. How can I do so? I use CentOS 5.6 #!/bin/bash tar -Pcf ...
2
votes
3answers
376 views

How does setting the Setuid bit affect shell scripts that run on boot of the system, before any login has occurred?

Would enabling the setuid bit on a shell script make a difference at all when that script is run on boot? Who would the effective user be?
6
votes
1answer
483 views

What is wrong with this shell script?

The service for a piece of software I just installed (from a provided .deb file) on a Ubuntu server won't start. When running the service I get the error: /etc/init.d/matrixsa: 44: Syntax error: "}" ...
7
votes
3answers
11k views

How to set global environment variables at boot through a script, and have them available for an application that runs before login?

I have a service that runs at boot, and in that service it calls a bash script in the background that exports some environment variables. The problem I'm having is that those environment variables ...
2
votes
4answers
2k views

Parallelizing a for loop with very large number of iterations

I want to parallelize a for loop where the number of iterations in the loop can be very large such as 10^6. So ,it will be better if I can create threads rather than process. How to do it? The code is ...
9
votes
2answers
3k 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 (( ...
8
votes
2answers
16k 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?
3
votes
2answers
104 views

dealing with un-executable commands inside a shell-script

I am given an assignment like this: Write a Bash script that takes a *.tex file as command-line argument and checks if the file executes correctly without any errors. If it executes without any error ...
-1
votes
2answers
3k views

clear tmpfs in my case

I am on a Ubuntu machine. I have make a directory under root directory, by: $ sudo mkdir /hello $ sudo mkdir /hello/bye Then I mount tmpfs with size 1024M to /hello/bye by: $ sudo echo "tmpfs ...
2
votes
2answers
335 views

How do I prevent a script from terminating when the shell exits?

In particular, I created a script to start Firefox which I double-click and choose 'Run in terminal,' but when the shell exits Firefox is killed immediately. How do I prevent this from happening?
3
votes
2answers
631 views

Simple shell script needed to move database files

I am using MySQL on Ubuntu machine. I need to physically move around the database files under /var/lib/mysql/ (this is where all the databases directories are located). Say I have two databases, ...
0
votes
2answers
479 views

getting file name from the input path to the file in shell scripts [duplicate]

Possible Duplicate: Best way to remove file extension from a string? Say that I have a script called script.sh In the shell I type script.sh ../folder/file.py and in that file I know ...
2
votes
5answers
663 views

How to find out if PWD contains spaces or non-English letters?

I created an environment variable: WD=`pwd` How can I check if it contains spaces or non-English letters?
5
votes
4answers
2k views

How to perform a sed in-place substitution that only creates backups of files that were changed?

I ran the following to replace a term used in all files in the current working directory: $ find . -type f -print0 | xargs -0 sed -i'.bup' -e's/Ms. Johnson/Mrs. Melbin/g' This performed the word ...
6
votes
3answers
763 views

How enable safeguards for CPU temperature

My desktop has a nasty habit. When I have several high intensity applications running and my CPU is at maximum usage for a period of time, the core temperature rises and my computer auto-shuts off. ...
1
vote
3answers
344 views

Returning a portion of a line matching a pattern

I have a file (file_name) which contains exactly one occurrance of the string "Result: " at the start of a line. I want to print all the characters after the string "Result: " in that line until I ...
12
votes
7answers
12k 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
3
votes
1answer
1k views

How to use a value returned by a script in grep filter?

I'm on FreeBSD's csh. I want to form a row with domains of one user and then use this row to filter exim maillog to get only mail from/to this user's domains. I wrote a script to form a row from a ...
2
votes
2answers
744 views

Adding 2 lines in text file stanza

I have a text file divided in stanzas. Each stanza starts with a [keyword] and ends with a blank line. Something like this: [keyword1] param1 = value1 param2 = value2 param3 = value3 [keyword2] ...
7
votes
7answers
2k views

Bash - Continuous String Manipulation

#!/bin/bash FILE="$(basename "$1")" FILE="${FILE/%.jpeg/.jpg}" Is there anyway to glue these two lines together into a one-liner?
1
vote
3answers
667 views

Writing a shell script to take output of an executable and perform some calculation

I am not much familiar with shell script. I want to write a shell script for the following pseudo-code: min=some garbage value for(i=1 to N){ // N and n will be taken as input for the shell script. ...
1
vote
3answers
674 views

To list out duplicate filenames in a listing?

bash-3.00$ cat f.txt -rw-r--r-- 1 mukesh other 102 Nov 5 18:32 f1.txt -rw-r--r-- 1 mukesh other 19 Nov 5 18:32 f2.txt -rw-r--r-- 1 mukesh other 204 Nov 5 18:32 ...
19
votes
4answers
9k views

How do I exit a script in a conditional statement?

I'm writing a bash script where I want to exit if the user is not root. The conditional works fine, but the script does not exit. [[ `id -u` == 0 ]] || (echo "Must be root to run script"; exit) ...
3
votes
2answers
751 views

Move with possible rename

Is it possible to construct a command that will move the file to another directory and if the same file is already there, generate some random string that is not in the name of some file in the ...
13
votes
3answers
472 views

When does `ls -s` print “0”

Of course, the standard way of testing if a file is empty is with test -s FILE, but one of our clients have received a script containing tests like this: RETVAL=`ls -s ./log/cr_trig.log | awk '{print ...
3
votes
3answers
3k views

What are the significant differences between different shells? [duplicate]

Possible Duplicate: What are the fundamental differences between the mainstream *NIX shells? If I write a shell script in bash, will it run in ksh as well? if not, what are the ...
6
votes
8answers
5k 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 ...
-1
votes
1answer
192 views

Timer in bash script not accurate when using it with longer times

#!/bin/bash # get user input ZENITY1=$(zenity --entry --text ' 1m: 60s\n 5m: 300s\n10m: 600s\n15m: 900s\n20m: 1200s\n25m: 1500s\n30m: 1800s\n35m: 2100s\n40m: 2400s\n45m: 2700s\n50m: ...
2
votes
1answer
743 views

What does the syntax of these echo commands mean?

I tried to use a copied script, which includes the following command echo "rc $2" > $WORKDIR/out.dat I can guess it tries to output some contents to file out.dat. But what does rc $2 mean? It ...
1
vote
3answers
324 views

ksh cannot cp from location with space in it?

I am trying to do the following in ksh but keep getting cannot stat message for the cp command: JMX_ROOT=/bfs-build/build-info/mep_mainline-Linux.latest/core/mainline/automation ...
1
vote
1answer
1k views

Concatenate multiple strings with spaces in them?

I am trying to do the following in ksh shell: JMX_ROOT=/bfs-build/build-info/mep_mainline-Linux.latest/core/mainline/automation SMOKE_JMX_LOCATION="$JMX_ROOT/\"Smoke Set\"/*.txt $JMX_ROOT/\"Smoke ...
1
vote
2answers
2k views

Why doesn't the following work?: while true; do “$my_command”; sleep 1; done

Following up on the top answer to this question, I thought I could: 1) Define a command: cmd='for x in $(my_command | grep keyword | cut -d" " -f1); do command2 "arguments" $x; done' 2) Run it ...
3
votes
2answers
5k views

Strip trailing whitespace from files

The answer to removing trailing whitespace with sed has most of the answer, but I want sed -i 's/[ \t]*$//' $1 to be able to take arbitrary number of file arguments as a shell script on the command ...
2
votes
2answers
405 views

Simple wrapper scripts spawning 100s of bash processes

I have this kind of GCC multilib wrapper set up: #file: gcc #!/usr/bin/env bash gcc -m32 "$@" which essentially just wraps a 64-bit multilib gcc to act as a non-multilib 32-bit gcc. When I build ...
6
votes
4answers
3k views

Building paths robustly

Say I have several variables in a shell script (e.g. in zsh): FOLDER_1, FOLDER_2, etc. These variables refer to folders descending from /. For example, if I have a path /home/me/stuff/items the ...
11
votes
3answers
4k 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?
14
votes
3answers
860 views

Newlines in filenames

I understand and accept the premise that defensive1 shell scripting is both prudent and, in the longer term, more sustainable. Many of the answers to text processing questions here follow this ...
2
votes
1answer
634 views

How to read a hexdump byte by byte in bash or awk?

This is the hexadecimal output of an IPv6 TCP packet captured with tcpdump: 6000 0000 0018 0620 0000 0000 0000 0000 0000 0000 0000 0001 0000 0000 0000 0000 0000 0000 0000 0002 *0026 0026 0000 ...