Shell Scripting





The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.

Share tech news, updates, or what's on your mind.

Sign up to Post

I am trying to create a Robocopy script that will copy files from one location to another location and purge the source files based on the timestamp. The script runs every hour at :00 and it copies the files to another location but, I want it to only delete the source files that are older than 15 minutes prior to the top of the hour. For example, the script runs at 11pm and I want it to only delete files older than 10:45pm. This is what I have currently in my script but, I don't know what switch(es) to use to do the file delete based on timestamp.

robocopy "\\\Share\CX" "D:\FC_Import\CXM" /S /E /SEC /MOV /COPYALL /V /NP /LOG+:"d:\Robocopy.txt" /R:3 /W:5
Expert Spotlight: Joe Anderson (DatabaseMX)
LVL 13
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Hi Shell/Bash experts,

Would like to make an entry to a file in linux but only if it doesn't exists in the file please

Below is the sample entry that I would like to make in a text file /tmp/dbentry.txt

Thanks in advance
$VAR = “”

Need to extract 76.0. (1st 2 decimals)

I am using ${VAR:0:4} which returns 76.0

but if if $VAR has
3 digits (The first number can be 3 digits)

using ${VAR:0:4} does not work.
I have a bash script that returns the value.

For example,

bash $HOME/repos/newton/chromeDriverUpgrade.bash

I get 75.0.0

I assigned to a variable

CHROME=bash $HOME/repos/newton/chromeDriverUpgrade.bash

echo $CHROME

It does not print anything.

Can you help if anything is wrong.
Need to include (^) in the bash script variable.

I use the below to get the google chrome version

CHROMEDRIVER_VERSION=$(cat package.json \
  | grep chromedriver \
  | head -1 \
  | awk -F: '{ print $2 }' \
  | sed 's/[",]//g' \
  | tr -d '[[:space:]]')


prints out ^75.0.0

I use the below sed command to update package.json. The sed command updates the chromedriver if the value is 75..0.0 from the JSON file but if the chromedriver value is ^75.0.0, it does not update.

sed -i '' -e "/\"chromedriver\": /s/$CHROMEDRIVER_VERSION/$VER2/" package.json;

Please help
Hello all,
I have a directory on my macOS that every single name has a .dns at the end if it. for example:

Is there a sek, awk, perl or something I can run to remove the .dns file extension and leave it at
I dont care about preserving it, just change it all

They are located in this type dir structure

Thank you,
How can i write a shell script on RHEL 7.4 that does that counts number of line in netstat and if total lines exceeds 200 lines
send an email alert to "" with the number of lines and actual listing.


I want to schedule this script to run every 5 minutes via cron to monitor a connections performance issue.

Bash script inside package.json does not run when i use the command npm install.

I am in my local directory. I have package.json with the scripts section below

"scripts": {
    "preinstall": "echo preinstall",
    "UpgradeChrome": "bash chromeDriverUpgrade.bash"

when i run npm install, it runs preinstall command but not the bash script .

Do you see anything wrong with the above scripts section?
Is there a way to test a bash script in a way that it doesn't actually make any changes but it will show you the changes it would have made (or display the commands it would have executed)?
These are the Linux versions I'm using.
SUSE Linux Enterprise Server 11 SP4
Oracle Linux Server 7.6
From linux (SUSE Linux Enterprise Server 11 SP4) I want to read url's from a site. The URL's are embedded as shortcuts on the web page.
The site name
Then I want to pass this URL as a variable into a bash script

Here is a sample of a URL I'm trying to get.
Note there are severs URL's with different server names, ports, and the last part (AppsLogin) can also be different.
Exploring SQL Server 2016: Fundamentals
LVL 13
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

VERSION="Google Chrome 76.0.3809.100" . (Need to split and get only the version (76.0.3809.100)
#echo $VERSION

IFS=' ' read -a VERSION <<< "$VERSION"
    if "${VERSION[@]}"= "76.0.3809.100"
        echo "${VERSION[@]}"

It is not working. Please help.
How can I get an expect script to read a password from a file so it can be used to pass it to a program that requires that password.
I currently used autoexpect to create expect scripts, but the passwords are then hard coded.
Note: My OS is Linux

here is a snippet from my expect script
send -s -- "cd \$ADMIN_SCRIPTS_HOME"
expect -exact "cd \$ADMIN_SCRIPTS_HOME"
sleep .1
send -s -- "\r"
expect -exact "applmgr@appsrvr:/u01/blah/SID/R122/fs1/inst/apps/SID_hostname/admin/scripts> "
sleep .1
send -s -- "adop phase=fs_clone"
expect -exact "adop phase=fs_clone"
sleep .1
send -s -- "\r"
expect -exact "Enter the APPS password:"
sleep .1
send -s -- "hard_coded_password\r"

I want to be able to read the APPS password from a file and use it in this expect script.
Hi everyone,

I am trying to zip in zfiles<state>.zip file multiple (1000s) files (ASCII files) and get a count of each zip file in most efficient way (without dirrectory names and compress better) in Unix Bash script ( just like it was in the DCL language below.  If anyone knows how to do the same zipping process in the Unix bash script please let me know. Any suggestions or examples would be appriciated!

Note:  statelist.txt file has all states to loop through

Here is the DCL code portion that I am trying to convert to Unix bash scripting:
define/nolog DATA dir1/data
define/nolog proglog DATA:ZIP.LOG

laststate := ' '

 open/read statelist STATELIST.TXT
 read/end = endread statelist laststate

if f$search("ASCIIFILE%''laststate'%%%%%.TXT").EQS " " THEN -

! Create ZFILES<state>.zip  by zipping all ASCIIFILE files and send ZIP.LOG to the mail list
ZIP  -9V ZFILES'laststate'.ZIP - 

if .not. $status
  open/append pglog proglog
    write  pglog "--- Error Zipping ASCIIFILE*.TXT files ---"
   close pglog 
       SAY " --- Error Zipping ASCIIFILE*.TXT files ---"
 mail/subject="--- Zipping all files ==> Failure ---"

Open in new window

I have a bash script that calls more bash scripts, and those call other bash scripts, and some scripts run while others are running.
Each script writes some information into the same log file (script name, host it is running on, start time, stop time)
For example, this is what I want in the log file

RUNNING /DIR1/DIR2/DIR3/for_database/POST_DB_REFRESH/ on lddscoradb01 Start Date & Time: Fri Aug  2 09:28:51 CDT 2019
RUNNING /DIR1/DIR2/DIR3/for_database/POST_DB_REFRESH/ on lddscoradb01 Start Date & Time: Fri Aug  2 09:29:03 CDT 2019

Sometimes I get this in the log file, which I don’t want.
RUNNING /DIR1/DIR2/DIR3/RSYNC/ on hostname.apps.devRUNNING /DIR1/DIR2/DIR3/RSYNC/ on Start Date & Time:  Start Date & Time: Fri Aug  2 09:36:07 CDT 2019

I have scripts that run simultaneously, and I believe this is the crux of the problem. Multiple scripts can be writing at the same time.
Is there a way to lock the log file temporarily so that only that script can write to the log file, while at the same time it doesn’t cause a problem for the other scripts?

# This is the code at the top of the scripts
FILENAME=`basename $0`
echo ""
echo -n "RUNNING ${DIRECTORY}/${FILENAME} on ${SHORT_HOSTNAME}" | tee -a /DIR1/DIR2/DIR3/SCRIPT_CONTROL_LOG/master_script_control.log
echo " Start Date & Time: " | tee -a  …
Hi everyone,
I am trying to create a Menu script in Unix bash. A few issues that I am experiencing are:
1. Each Menu selection (#1,#2, and #3) ends right after I run each selection and the menu exits out back to the command line. I would like the Menu to ask a user if a user would like to continue with other selections (for example #2 or #3 if #1 was run in the first place) or exit the menu. Right now the Menu just exits after each selection without asking anything.  

2. In part of the selection #1, I would like to capture a value entered for "filecnt" (can be any number but 0) in the $parfl (parameter file). Right now this script allows to enter the value but that this value for some reason is not captured in the $parfl parameter file. The script creates the $parfl file but it is empty.  

What am I missing? Any suggestions or examples would be greatly appreciated. Thank you!

Open in new window

export DATA=/home/data
export EXEC=/home/exec

export parfl=${DATA}/parameterfile.txt
chmod 777 $parfl

# Menu

echo "             Control Menu"
echo " "
echo "Selection 1: Run Script1"
echo "Selection 2: Run Script2"
echo "Selection 3: Run Script3 "
echo "Selection 9: Exit System"
echo -e "\n"
echo -e "*** Make a selection from the Menu above and press enter ***"
read select

select=$(echo $select | tr '[:lower:]' '[:upper:]')
if [[ (("$select" <…
Trying to build sed command with variables and run the command (in a bash script on linux).
Ideally id like to substitute the string in the file. But I will settle for deleting it and replacing it.

LINE_TO_CHANGE=`grep -Hn ${CONTEXT_VARIABLE} ${FILE_TO_CHANGE} | awk -F'[:]' '{print $2}'`

grep -Hn ${CONTEXT_VARIABLE} ${FILE_TO_CHANGE} | awk -F'[:]' '{print $2}' returns 205

One of these two lines could exist in the file (between the double quotes on the next two lines)
"         <sslterminator oa_var="s_enable_sslterminator" customized="yes"/>"
"         <sslterminator oa_var="s_enable_sslterminator">customized</sslterminator>"

I want to change the entire line to (between the double quotes on the next line)
"         <sslterminator oa_var="s_enable_sslterminator">#</sslterminator}>"
I am trying to get the version of the chrome using the below bash script.


getChrome() {

     if [ -z "$1" ]; then
             echo -e "No arguments specified\n"
     echo -e "Options:
             --version             Displays the version of the chrome
             --location            Displays the location of the chrome profile folder.
             version             Displays the version of the chrome
             location            Displays the location of the chrome profile folder.

             return 0

         if [[ $1 == version]]; then
             google-chrome --version
             echo 'test'
             return 0

         if [[ $1 == location]]; then
                      chromeLocation = ""/Users/user1/Library/Application Support/Google/Chrome/Default"
                      return 0
export -f getChrome

when i run the command in the bash terminal getChrome --version, it is not printing the version. Please help.
When you have script_1 call script_2, which in turn calls script_3, Is there a way to auto indent echo statements depending the order of what script calls the other? It would be nice if the script could also print the script that called it.
I'm using bash on linux.

example output
Running script_1
   Running script_2
      Running script_3

example output 2
Running script_1
   Running script_2  Called by script_1
      Running script_3 Called by script_2
We're getting very strange behaviour running a robocopy script for copying files. When we run this script from an elevated command prompt which defaults to C:\windows\system32 :

robocopy.exe ""\\servershare\foldername\subfoldername"" ""C:\localfoldername\localsubfoldername"" /E /COPY:DATSOU /V /NP /R:10 /W:30

it creates any folders and subfolders that exist in the source in the destination but doesn't copy any files within them with this error:

Copying NTFS Security to Destination File \\servershare\foldername\subfoldername\xxx.dotx
A required privilege is not held by the client.

when I do a cd .. to go back to C: and then run the same robocopy script it runs fine and copies the files as well!

Looks like we have multiple versions of robocopy.exe around. There's one in c:\windows\system32 and another in c:\windows\SysWOW64. Thought it may have been defaulting to the version in C\Windows\SysWOW64 from the root of C: so to test that we navigated to C:\Windows\System32 and the ran the script preceded by C:\\Windows|SysWOW64 and it still failed so we're a little flummoxed! We could potentially switch to xcopy but would prefer to stick to robocopy as it's easier. Can someone shed some light on this please?
Introduction to R
LVL 13
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

How to change variable in all scripts under a directory and all sub-directories.
I'm open to using anything to get this done. I've been struggling for two days on this, and I need it done today.
I have GNU sed version 4.1.5 (upgrading is not an option at this point as the SA is out for training)

For all files under /.hidden/cloning/EBS_APPS_CLONING_SID1/
They can have different entries for BASE_DIR

I want to change it to all lines beginning with BASE_DIR to a new BASE_DIR

This is what I have so far in my script
(I use a find statement and loop through it, which is where the $f comes from)
echo "sed -i '/${SEARCH_STRING}/c\\${ADJUSTED_REPLACEMENT_STRING}' ${f}" <-- when I copy and past the result of this echo statement it works but I don't get the quotes. for example I get BASE_DIR=/.hidden/cloning/EBS_APPS_CLONING_DBAD1 but I want BASE_DIR='/.EBSclone/clone/EBS_APPS_CLONING_DBAD1'

Open in new window

sed -i '/${SEARCH_STRING}/c\\${ADJUSTED_REPLACEMENT_STRING}' ${f}  <-- this doesn't work in the script
(openssl x509 -text -noout; openssl x509 -text -noout) < ./snp_ssl.pem | more

Open in new window

What does the above command mean in nix command line.
Hi bash experts.

Could you kindly help with this please.

  1. Have a json file which I would like to automate replacing some values.
  2. Value would come from a property file.
  3. I'm trying to avoid introducing a token in the source json file and use sed to replace it.
  4. Would really like to check for name and then replace value portion, may be sed will use regex
  5. Since I dont have control on the file, I prefer this.
  6. Very worst case I can introudce tocken and use sed to replace if that is safe and simple

Have attached
  1. json file (before)
  2. json file (after)
  3. sample property file

Thanks in advance

I need to read file from Unix directory as soon as the file is placed in it and then subsequently archive it.

Also to mention that I would not know the file name in advance, i.e. file names would be different every time.


Does anyone have a script to share that will check all groups in the domain and output if the " Manager can update membership list" is checked off ( enabled ) .
The CSV should include the group name and the samAccountName listed that can update the group .
Hello experts,
I am new in powershell and I would like to know which command are related to:
1-Parse current dir as variable (example in cmd %cd%)
2-Echo specific variable (example in cmd echo %variable%)
3-Log ouput of script (example in cmd > test.log 2>&1)
Thank you in advance for your help.

Shell Scripting





The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.