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

learn more… | top users | synonyms (2)

3
votes
3answers
280 views

Press any key to pause shell script, press again to resume

I've written a shell script for testing an API that copies files and echoes its progress after each one. There is a two second sleep between each copy, so I would like to add the ability to press ...
3
votes
5answers
362 views

Stripping directory paths to get file names

What I am doing is converting mp3's with LAME. I already have an old script I wrote that works but I want to add to it this ability--to no longer delete the file but instead save it in a new root ...
3
votes
2answers
176 views

Shell script to assign values from a lookup table is too slow

I'm trying to assign some variables from a lookup file with a shell script. I have something working but it seems unnecessary slow. Script: while read line do ...
3
votes
3answers
279 views

Pipe in a shell script

I have done this little script : #! /bin/sh path="foo" while touch $path ; do path=./${path} done echo "echec avec le nom $path qui fait" echo $path|wc -c echo "caracteres" How can I ...
3
votes
4answers
311 views

dd remove range of bytes

Given this file $ cat hello.txt hello doge world I would like to remove a range of bytes to end up with this $ cat hello.txt heorld I would like to do this with dd if possible. The reason is ...
3
votes
1answer
331 views

Why does the history command do nothing in a script file?

If I create a file containing #!/bin/bash history and run it, I get no output. Running it manually in a terminal or sourceing the script file does produce output. Why does history not work from ...
3
votes
5answers
678 views

Quoting and escaping

I have a shell script, the arguments of which are collected in quote as a single argument and passed to a perl script. /usr/local/API/check_api.sh "-D x.x.x.x -C Test_Internal_Cluster -u user -p pass ...
3
votes
2answers
4k views

bash script error: source: not found

I must be missing a fundamental understanding about sourcing files in bash. I've tried the different approaches that seem like they should work, but I still get this error source: not found. In my ...
3
votes
2answers
864 views

Bash Script To Find Debian Release Number from /etc/debian_version

The following is what I'm currently using for testing the version of Debian. Instead of doing it this way, can I make it more succinct? #!/bin/bash VERSION=NONE if [ $(cat /etc/debian_version) == ...
3
votes
3answers
718 views

Bash script to delete a file called index.html recursively

I need a bash script (not sure how to write the actual .sh file) which I could set to be run by cron every minute and that would delete files with the name index.html that are in a specific directory ...
3
votes
1answer
167 views

CPU is free, yet bash script not utilizing all CPU resources

I ran a simple script to generate a large (10000000 lines) csv file with 6 fields in which some fields changed in each line/row, using a while loop. The machine had all (32) CPUs free, lot of RAM (~31 ...
3
votes
2answers
1k views

Obtain exclusive read/write lock on a file for atomic updates

I want to have a file that is used as a counter. User A will write and increment this number, while User B requests to read the file. Is it possible that User A can lock this file so no one can read ...
3
votes
3answers
10k views

Shell script to send an alert mail if disk usage exceeds 90%

I am new to shell scripting and want to implement a script on my server which will automatically send email alerts if the disk usage goes over 90% and second email alert when it goes over 95%. My ...
3
votes
2answers
242 views

Clarification regarding behavior of shell script along with pipe

I need help understanding the behaviour of the following shell script. #!/bin/bash echo "First more" more $1 echo "First echo" echo $1 echo "Second more" more $1 When I run the following command: ...
3
votes
3answers
8k views

How to take 'password' like input in shell script? [duplicate]

Possible Duplicate: How to hide commands typed in a Linux shell? Reading passwords without showing on screen in Bash Scripts I would like to take a password as input, but I want user to ...
3
votes
4answers
362 views

Run a given command on every subpath of a long path

Say I have a long path such as: /a/b/c/d/e/f I would like to run a command only on each of the subpaths of the path: e.g. If my command is cmd, I am looking for a one-liner that can do (perhaps ...
3
votes
3answers
3k views

How to search and replace text in all php-files in a directory and it's subdirectories

I am looking for a shell script that recursively traverses all .php files in a directory and performs a search & replace of a particular text pattern. The search pattern is quite long ( > 5000 ...
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 ...
3
votes
2answers
102 views

Unatended script execution in ubuntu terminal

I have installed Ubuntu on a VMWare virtual machine. When I got to installing a VMWare tools, I have stumbled upon a simple question: If I execute a script (perl script in VMWare tools case), is ...
3
votes
2answers
4k views

Get path of current script when executed through a symlink

I have a utility consisting of a couple directories with some bash scripts and supporting files that will be deployed to several machines possibly in a different directory on each machine. The ...
3
votes
2answers
121 views

Loop in WildCard as Input of Script

I want to use my script on many files sometimes. I used this for the purpose: for etminput in $1 do #process done But this just gives the first input. How can I do the process on every ...
3
votes
5answers
164 views

Look if a folder has some files of certain extention

Lets say I have a file structure: $ cd /home/Desktop $ ls -d */ Abc/ Qwe/ Zxc/ Rty/ $ Now I want to iterate through every directory and look if they have any .pdf files. Could some one please ...
3
votes
1answer
7k views

Bash command not found error

Why would the following script give following errors : ./check1.sh: line 10: Hi,: command not found ./check1.sh: line 21: syntax error: unexpected end of file check1.sh: #!/bin/bash subj="host ...
3
votes
3answers
453 views

Command for taking a screenshot to a new file

I want to configure a global hotkey to take a new screenshot and save it, say, somewhere inside my home folder. I can take a screenshot with ImageMagick with this command: import screenshot.jpg ...
3
votes
4answers
3k views

How do I select files in a bash script that do not have an extension?

Let's say I have files xinitrc, alphabetsoup, ieat.pie in the current directory. Need a bash script to select only xinitrc and alphabetsoup.
3
votes
3answers
191 views

checking data in columns when a data or some may be missing or present?

I am not sure if this possible. say i have columns like : Team Colour Game Rainfall PlayerName XYZ Blue Cricket Yes Kapil suppose i need to search ...
3
votes
5answers
667 views

changing pattern of a text file

A text file has contents something like chair table pen desk Now i want it to be changed and stored in a variable say var as below ('chair','chair'),('table','table'),('pen','pen'),('desk','desk') ...
3
votes
2answers
835 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 ...
3
votes
2answers
267 views

pulling strings out of mixed file of strings and binary

I'm trying to get my nokia backup file into an acceptable vcard format. It has valid vcard strings that appear in between binary data. For example: !@#JUNKCHARACTERS!@#!@BEGIN:VCARD VERSION:2.1 ...
3
votes
3answers
14k views

How can I call other shell script like a subroutine?

How do I call an other shell script and wait for its completion? I want to pass input arguments and receive returning result code. And continue running rest of codes.
3
votes
7answers
1k views

How to write repeated free-form strings to a file, as fast as 'dd'?

dd can write repeating \0 bytes to a file very fast, but it can't write repeating arbitary strings. Is there a bash-shell method to write repeating arbitary strings equally as fast as 'dd' (including ...
3
votes
1answer
272 views

How to use printf to show the results of a calculation?

My program is #!/bin/sh printf 'Please enter salary amount: ' read salary TAX="echo $salary * 0.15 | bc" NET="echo $salary + $TAX | bc" printf "Net Salary= %.2f" $NET printf "\n" But whatever I ...
3
votes
2answers
245 views

Shell script wrapper to prevent running command with no arguments?

There is program I use, say xyz, that has undesirable effects if I run the command bare with no arguments. So I'd like to prevent myself from accidentally running the xyz command with no arguments ...
3
votes
3answers
107 views

Count nul delimited items in file

I have a shell script which uses find -print0 to save a list of files to be processed into a temporary file. As part of the logging I'd like to output the number of files found, and so I need a way ...
3
votes
2answers
112 views

get result text from CLI command

I'm writing a bash script and I need to capture the text resulting from the output of the rsync command, for instance, the following command: rsync -av "$FOLDER1" "$FOLDER2" prints: ... sent ...
3
votes
1answer
382 views

How do I fill my password automatically from .bash_profile when running command as sudo?

I know I could just put something like sudo mypassword in my .bash_profile, but I don't want to run every command as root. I want password to autofill under following circumstances: only the ...
3
votes
2answers
156 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
3answers
729 views

AWK: Passing shell variables to awk

I am trying to pass a variable number of arguments from the shell script to a pattern recognition subset of a table. Here is my attempt so far: The file 'infile': ID,GROUP 1,GROUP2 ...
3
votes
1answer
2k views

Why does the filename contains question marks when I run a script on my remote machine?

I have a tiny script that simply fetches the current date, runs a PHP script and redirect the output (and errors) to a filename containing the current date. DATE=$(date +"%Y%m%d") ...
3
votes
1answer
713 views

What's the difference between “==” and “=~”?

Given a string and a hash returned by openssl, why using == doesn't match them correctly but =~ does? Background question: How can I force a script to use more resources?
3
votes
2answers
347 views

Moving large numbers of files and directories to a different directory

I'm trying to move the following move a large number of files that exist in the following structure to a /mnt/originals but I need to maintain the structure at the same time. I know cp would be a ...
3
votes
4answers
932 views

Replace all newlines to space except the last

How can I replace all newlines with space except the last newline. I can replace all newline to space using tr but how I can do it with some exceptions?
3
votes
2answers
206 views

How to make /etc/init.d script act like it's launched under X?

I'm trying to track down some quirkiness with a Java application that misbehaves when launched via a startup script in /etc/init.d , but runs fine when you open a GUI terminal window and run it ...
3
votes
2answers
931 views

How to find frequency of occurrences of strings contained in a file?

I have a file that contains a list of URLs of the form EDIT http://www.google.com/absd/siidfs/kfd837382$%^$&, www.google.com, google.com yahoo.com/list/page/jhfjkshdjf... I ...
3
votes
4answers
1k views

Align text to center with padding on both sides

Is there an easy way to align text to the center with padding on both sides, with the column width being the longest line from the input? For example, this: aaa bbbb c ddddd ee will turn into this ...
3
votes
2answers
2k views

How to safely reverse dns lookup on ip in a shell script

I'm trying to find out how to take in a hostname and pop out an ip address (reverse dns lookup). I came up with this but I'm not too sure it's safe across different linux distros: nslookup $ip|grep ...
3
votes
3answers
1k views

How to convert all .wav files in subdirectories to .aac using neroAacEnc?

I have a collection of music in .wav format, and would like to convert it to .aac/.mp4 using neroAacEnc. How do I do this? I am using Arch Linux x86_64 and Xterm.
3
votes
1answer
69 views

Using a ; or && with $@

I'm trying to do something with a cron wrapper and my cron looks something like * * * * * root /usr/bin/wrapper cd /tmp/ && ls Then wrapper looks like #!/bin/bash "$@" When I run it I ...
3
votes
6answers
214 views

Extracting help message from script itself

I want that my help message be extracted from the script itself. #!/bin/bash # # foo - do things # Author: John Doe <jhon@doe> # ---------------------------------------------- # SYNOPSIS # ...
3
votes
2answers
5k views

How can I trim the carriage return from text that is being piped in bash?

I'm using this command to get my last typed command: history | cut -c 8- | tail -n 2 | head -n 1 It works very well in bash, removing the line numbers, but there is one problem I have with it, (er, ...