Showing posts with label shell-script. Show all posts
Replace a string with an environment variable.
Many times its required inside a shell script that you need to replace a string in a file with a value which is present in a environment variable.Most of the time people will face this in their daily work life if they work on unix exclusively.Below is the way to do it.
echo $LINE | sed -e "s/12345678/${replace}/g"Small test below:
export replace=987654321 echo X123456789X | sed "s/123456789/${replace}/" X987654321X
Passing a bash variable to awk in a shell script
Many a times there is a need for passing shell variables inside a shell script to awk.below is the way we have to pass it and the execution is done on solaris unix:
> setenv X "hello"
> echo $X | nawk -v str="${X}" '{print str}'
hello
>
Moving a process from background to foreground
In unix, if we run a process(a shell script).It will not return to the terminal untill the script ends.lets say there is a script temp.sh
>cat temp.sh #!/bin/sh for i in 2 3 3 3 45 do sleep 10 echo $i doneThis process will sleep for 10 seconds for every iteration and prints the value of $i.
If I run the process as below:
> ./temp.sh 2 3 3 3 45 >If you see above ,you can observe that the script took 50 seconds to return to the terminal.
So better for us to run the process in the background.
> ./temp.sh & [1] 20323 >The number which is present in the square braces is the job id and the number outside the square braces is the process id(pid) Now if you see ps output you can see the process as running or also in the output of jobs
Yes,the command jobs will return all the background processes that were stared by you.
>jobs [1] + Running ./temp.sh [2] - Running ./temp.sh [3] Running ./temp.sh [4] Running ./temp.shfg is the command to bring it back to the foreground as shown below.
>fg 1Now press CTRL+c. As seen above I have ended the process and it no longer exists. Now if I again run the command jobs
>jobs [2] + Running ./temp.sh [3] Running ./temp.sh [4] - Running ./temp.sh>
secure copy in a shell script
I have a list of file names in a file. The file names have many extensions. But I am only interested in file names which have ".txt" as their extension.The solution is :
while read rname do ext=`echo $rname|awk -F"." '{print $NF}'` if [[ "$ext" == "txt" ]] then scp $scp_user@$scp_server:${rname} /a/b/c/ fi done < filenamelist.txt
Shell Script for taking backup
This below script is found to be very useful for me.It does four things specifically.- Creates a folder by date.
- Compresses the files for backup
- Sticks them in your backup directory
- Clears out backups older than 3 days.
#!/bin/bash cd /home/backups mkdir $(date +%Y-%m-%d) cd /opt/ tar -pczf /home/backups/$(date +%Y-%m-%d)/opt.tar.gz code cd /var/ tar -pczf /home/backups/$(date +%Y-%m-%d)/var.tar.gz work cd /home/backups/ threedaysago=`date -d "3 days ago" +%Y%m%d` for backup in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] do backupdate=`echo "$backup" | tr -d -` # remove dashes if test "$backupdate" -lt "$threedaysago" then rm -rf "$backup" fi done exit
Breaking an infinite loop in a shell script
How can I stop an infinite loop in a script?Or in other words it can also be said like How to send a signal for an infinite loop in script and exit that loop gracefully?
It's quite simple. Below is a test script which will run into an infinite loop:
echo "My pid is: $$" finish=0 trap 'finish=1' SIGUSR1 while (( finish != 1 )) do stuff sleep 5 doneExecute this and if you want to test and while the script is executing in the background, do as below:
kill -SIGUSR1 pidWhere pid is the process id of the script.If the signal is raised during the
Sleep, it will wake up (sleep sleeps until any signal occurs) and exit the loop only gracefully.
Subscribe to:
Posts (Atom)