bash

How would I use grep on a single word in UNIX?

I want to run a script in unix that will look for a specific pattern inside a passed argument. The argument is a single word, in all cases. I cannot use grep, as grep only works on searching through files. Is there a better unix command out there that can help me? ...

bash script writing shell commands

Why is that some commands require the echo statement but others can simply be written without: #!/bin/bash aptitude upgrade echo "mysql-server-5.1 mysql-server/root_password password $DB_PASSWORD" | debconf-set-selections ...

Insert text between two marker lines in bash

I have some lines stored in a text file called bashrc_snippet. I would like to insert them into .bashrc. Since I sometimes change the contents of the text file I would like to be able to re-insert them in the .bashrc-file. To do this I want to use marker lines: # User things HISTSIZE=1000 #START alias ls='ls --color=tty' ... some more ...

Best/conventional method of dealing with PATH on multiple UNIX environments

The main question here is: is there a standard method of writing UNIX shell scripts that will run on multiple UNIX platforms. For example, we have many hosts running different flavours of UNIX (Solaris, Linux) and at different versions all with slightly different file system layouts. Some hosts have whoami in /usr/local/gnu/bin/, and ...

How to do exponentiation in bash

I try echo 10**2 it prints 10**2. How to make it work? ...

Writing to a file through a bash script

I want to write a bash script which takes 2 arguments, a and b. Based on the argument, it writes in section a or section b of a text file. Output file is a txt file something like this: common content.... section a: <everything should be written here when I specify option "a"> section b: <everything should be written here when I specif...

Bash copying files with variables

Hi New to bash scripting, I'm writing a script to copy my TV shows accross from a download folder to an archive folder. So far I have this: find `*`show1`*`.avi | cp \"" $0 "\" "/mnt/main/data/tv/Show1" find `*`show2`*`.avi | cp \"" $0 "\" "/mnt/main/data/tv/Show2" I understand this is not the best method, but my skills of bash ...

How to set bash trap again in trap code?

Hi, I have a bash function that is called must be called by an EXIT trap after the first time that it is called. The function sets the trap again to fire as soon as the function exits. echo 0 > .i function launchNextExperiment { ( # Run in nested subshell # Implement a mutex lock, not shown j=`cat .i` if [ $j -lt $k ] then t...

Bash, stdout redirect of commands like scp

Hi, I have a bash script with some scp commands inside. It works very well but, if I try to redirect my stdout with "./myscript.sh >log", only my explicit echos are shown in the "log" file. The scp output is missing. if $C_SFTP; then scp -r $C_SFTP_USER@$C_SFTP_HOST:$C_SOURCE "$C_TMPDIR" fi Ok, what should I do now? Thank you ...

Bash loop ends unexpectedly looping without showing why

I have a bash script with a code like this: echo "EXECUTING TASK 1" sort -r scripts/sh/test-list | while read fn; do sh -vx scripts/sh/test-web-task.sh "$fn" done echo "EXECUTING TASK 2" sort -r scripts/sh/test-unit-list | while read fn; do sh -vx scripts/sh/test-unit-task.sh "$fn" done In test-web-task and test-unit-t...

detect if something is modified in directory, and if so, backup - otherwise do nothing

I have a "Data" directory, that I rsync to a remote NAS periodically via a shell script. However, I'd like to make this more efficient. I'd like to detect if something has changed in "Data" before running rsync. This is so that I don't wake up the drives on the NAS unnecessarily. I was thinking of modifying the shell script to get the ...

Use bash-type `${i%.mp3}` syntax with xargs?

Example of usage (contrived!): rename .mp3 files to .txt. I'd like to be able to do something like find -name '*.mp3' -print0 | xargs -0 -I'file' mv file ${file%.mp3}.txt This doesn't work, so I've resorted to piping find's output through sed -e 's/.mp3//g', which does the trick. Seems a bit hackish though; is there a way to use the...

bash: print: command not found

I've just added a .profile to my bash shell. However, I've noticed the error "-bash: print: commant not found" keeps on appearing even if you are changing to a valid directory. My .profile just contains a few exports. [rob@mypc:/home/rob]cd apps -bash: print: command not found [rob@mypc:/home/rob/apps]cd util -bash: print: command not ...

in a bash script, get the pid of a background process

Is it possible to know the pid of the iwevent process in the following bash script: #!/bin/sh ( iwevent | logger -t IWEVENT ) & echo the pid is: ??? Note that iwevent run until ctrl-c signal. FYI. I run this script in a /etc/network/interfaces "up" statement and I want to kill the running iwevent process in the related "down" stateme...

change names of all files in a directory to random strings

Purpose: change names of all files in a directory to random strings of the same length list this files in another file (with a possibility to make from it sql query ) I think bash would be great for it, but I have no idea how to do this, can you help me? ...

How can I list all unique file names without their extensions in bash?

Hi, I have a task where I need to move a bunch of files from one directory to another. I need move all files with the same file name (i.e. blah.pdf, blah.txt, blah.html, etc...) at the same time, and I can move a set of these every four minutes. I had a short bash script to just move a single file at a time at these intervals, but the ne...

Verify if copying a folder went ok

Hi, I'm pretty new to bash, but I have the following script that does a simple operation: copying a folder. In shell, I would type sudo macaco NewFolder and this is the bash script: #!/bin/bash wwwPATH="/var/www" bitMotorVERSION="0.0.0" targetDir="$wwwPATH/$1" cp -r /var/fw/bitMotor/$bitMotorVERSION/ $targetDir This works fine! No...

Open gnome terminal programmatically and execute commands after bashrc was executed

I try to build a little script to start my development environment. For that task I try to open a gnome terminal with several tabs where automatically the rails server and autotest is started. But gnome-terminal --tab -e "rails server" --tab --tab does not work ("error creating the child process"). Also gnome-terminal --tab -e "ba...

How to get the length of a variable in bash?

I currently have something of the form abc=`find ~/$folder .. etc I'd like to know how to get the number of items in abc: abc_length = ? Thanks ...

term inside emacs:: how to turn "word wrap" off

In setting up my personal Linux command line development environment, I want to use term inside emacs b/c then I can switch to 'line mode' and copy/paste the output into any other buffer. However, when I run mysql inside term inside emacs, the pretty sql tables still word wrap according to the width of that emacs window :(. I was hoping...