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

hi how can i add user to a group in linux i what to move user from root group to another user
Amazon Web Services
LVL 19
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

I have a set of images contained in a folder and I would like to cast them to a TV.

I would like the images to change every 30 seconds and then repeat.

Any advice would be useful?

I'm attempting to run a bash script to do a ping sweep on the network.
See screenshot of the bash script.

When I run the script it gives me an error. I'm doing this from Kali Linux.  See screenshot of the error. What am I doing wrong here?
HI Bash/shell experts,
How to extract part of the string and store them in variables

I use curl with grep and get below line
<a href="hip-hui-amzn-6-c20191018-p20191028-1.1" >hip-hui-amzn-6-c20191018-p20191028-1.1</a><br />

Open in new window

we need something like this pls

Open in new window

Could you help with a bash script in a line or two please
Dear experts, I just can't figure it out...

Below my script..

set -x


GetActiveServer() {
ssh -T "$user"@"$server" <<EOF
echo \$env
echo \$server
/u01/app/ status resource -t > /tmp/resource.tmp
ActiveServer=$(cat "/tmp/resource.tmp" | grep -A 1 OracleDIAgent | grep "ONLINE  ONLINE" |  awk '{ print $4 }' | tail -1)
echo ActiveServer for "\$env" = \$ActiveServer

######## SIM #########

######## TST #########

Open in new window

And this is the output:

+ user=odiadm
+ server=argexa02sim02
+ env=SIM
+ GetActiveServer
+ ssh -T odiadm@exa02sim02
++ grep 'ONLINE  ONLINE'
++ grep -A 1 OracleDIAgent
++ awk '{ print $4 }'
++ tail -1
++ cat /tmp/resource.tmp
cat: /tmp/resource.tmp: No such file or directory

ActiveServer for  =
+ server=argexa01tst01
+ env=TST
+ GetActiveServer
+ ssh -T odiadm@exa01tst01
++ grep 'ONLINE  ONLINE'
++ awk '{ print $4 }'
++ grep -A 1 OracleDIAgent
++ tail -1
++ cat /tmp/resource.tmp
cat: /tmp/resource.tmp: No such file or directory

ActiveServer for  =

Open in new window

It appears a whole lot is going wrong and I tried quit a bit.

When I go to the server and execute the cmd it works fine:

-rw-r----- 1 odiadm   odiadm        5652 Oct 30 10:46 resource.tmp
odiadm@exa02sim02:/tmp $  cat /tmp/resource.tmp | grep -A 1 OracleDIAgent | grep "ONLINE  ONLINE" |  awk '{ print $4 }' | tail -1

I understand the escaping of the remote vars more or less (though that doesn't work out either), but why is the /tmp/resource.tmp file not found?

Can you please help?

Is there a good tool to convert PowerShell script to a exe file?

Hi Bash/Shell experts.

Kindly pls help

I have a variable which get value like below. In some scenario I get only one mac address, in other scenario I get two. But I want only one :)

06:d1:eb:cf:5f:3c/ 06:f2:21:8a:9c:0a/

My request, How can I get only one even if I get two or more. I tried below, but I think, it will break if I get only one
MAC_ADDRESS=$(echo $MAC_ADDRESS | cut -d / -f 1)

could you help me the best approach please

thanks in advance
I have two accounts with the same hosting provider.  They allow SSH access.  I created backup copies of websites on one account that I need to move to the other account.

I can move them individually, explicitly with "wget" but if I try to just pull all files from that one directory it fails.

Here is the command I am using, there are NO other sub-directories, just a bunch of files in one directory that I want to copy over from Account A to Account B.

wget -nc --level=1 --recursive --no-parent --accept jpa <url>/backups/

All I get when I run that command in the SSH portal is an "index.html" file and nothing more.  No error, just that one file (which is then automatically removed because it is not on my accept list).

I see other people (in general) are having a similar issue on StackOverflow (for example).

Server restriction maybe?  Problem with the command?

Thanks. :)
I'm consistently failing on the following:
- I have a bash script, say, a simplified content of which is listed below
- it is called with 0 or 1 argument, the normal value of which is "nomail" when I don't want a mail to be sent in the process
- the script first checks if nomail was provided as an argument and if yes gives the same value to the internal variable NOMAIL (otherwise empty). In the version below, it exports the value, as one of my (failed) attemps to solve the issue
- then runs the script somescript as user postgres (thru su)
- then launches another script mainscript with as argument the content of the NOMAIL variable

What happens:
- when running the example script from command line, everything works as expected, and the call to mainscript does indeed include the non-empty content of NOMAIL as as an argument
- but when running from cron, the call to mainscript is done with an empty value for NOMAIL!!!

(my first attempts were made without export, so I tried that just in despair!)



if  [ x$1 == 'xnomail' ] || [ x$2 == 'xnomail' ] ; then
      export NOMAIL='nomail'

su postgres -c "$LE_SCRIPT"

exec /root/ $NOMAIL

Open in new window

Hi Experts,

I've been working on this powershell script and need some help with a couple of things.  The script will be called from this vbscript to pass the computername or listofcomputers variables.

What I'd like expertise or help with is:

1. I've tested calling this from the VBScript Code in top comments section, but didn't get the computername variable to pass down.  (this would be done from the vbscript above, but wanted just a test wrapper to test).  EDIT:  If this is too difficult, I'd like to set it up to pull from a list of computers.

2. I'm first running "Test-MyConnection" (if) the computer is online, I'm then calling a executable.exe.  This is the part I'm having issues with - I don't necessarily want to run a .exe here, but call an SCCM advert (pasted below from client center), or, I also have a vbscript that runs the advert I could call out to.  This advert would exist locally on every computer.  The part I'm unsure about, or how to code is having this advert run on each computer in the list of variables

SCCM Advert Code:
 get-wmiobject -query "SELECT * FROM CCM_Scheduler_ScheduledMessage WHERE ScheduledMessageID='SCCM00001-SCCM00002-5D2960FE'" -namespace "ROOT\ccm\policy\machine\actualconfig"

Open in new window

Exploring SharePoint 2016
LVL 19
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

I have 2 scripts one is the Master_UpgradeScript.sql and the other is the scriptFileToRun.sql that I need to run to alter several schemas and add indexes and constraints to them. I feel like I am missing this: DEFINE schema_name=&1; from the scriptFileToRun.sql file and I don't know who this ties into the Master_UpgradeScript.sql.....

Could someone please verify if I need this (DEFINE schema_name=&1;) or not and if so please explain what it is doing and where it comes from? And how does it get into the ALTER script that needs to run?
Hi tesseract OCR experts,

I’ve just installed tesseract on my Raspberry Pi running Linux (Raspbain) and I’m trying to extract text from PNG screen shots taken on my phone.  (I have hundreds of these screen shots, all in the same size & format, taken over the last year using the LeafSpy Lite app, for the Nissan LEAF EV, and I'll be extracting text from all of them.)

The problem I have is, some of the text is not being extracted.

When I run this command:
$ tesseract sample1.png sample1
It produces sample1.txt (attached), which includes plenty of useful figures, but it excludes:
-      “11.84V” near the bottom left (nice to have this voltage figure, but not vital), and
-      “32.0%” at the bottom (I really need this SOC figure).

I tried feeding tesseract a negative (created with IrfanView on Windows) of the image, in case it was a black/white issue, but that gave the same output.
I tried cropping the 11.84V and 32.0% figures out to TIF files (see sample1_voltage.tif & sample1_soc.tif attached, also created with IrfanView on Windows) then running them through tesseract, and that:
-      failed for the 11.84V (see empty sample1_voltage.txt attached), but
-      worked for the 32.0% (see sample1_soc.txt attached).

I know bash and Perl scripting.  I don’t know Python, but Python is installed so it could be used if necessary, if someone else writes the code, but it's not my preference.
ImageMagicK is also installed, in case I need to use it for cropping or whatever.…
Within an ssh session on an instance, here is what I'd like to accomplish:

Steps in Script:
1.  Move a file (each file starts with "cfrtl4" - about 8 files total) from the remote systems /tmp directory to the etc/deployment-apps  
      directory as the SudoUser.
2.   Continue the same process until each "cfrtl4" file from the /tmp directory has been moved into the etc/deployment-apps
3.   Next, I'd like to chown the /opt/splunk/etc/deployment-apps directory as SudoUser:
       e.g. sudo chown -R splunk:splunk /opt/splunk/etc/deployment-apps
4.   I need to log out of SudoUser and log back in as splunkuser so that I can reboot the instance:
      sudo su splunk
5.   Now as the splunk user, I need to navigate to:  cd /opt/splunk/bin
6.   Once in the /bin directory, I need to issue a reboot command, e.g. ./splunk restart

#--- Execute the script from the /tmp directory ---
sudo mv cfrtl4_all_deployment_client/ /opt/splunk/etc/deployment-apps
sudo mv cfrtl4_all_indexer_base/ /opt/splunk/etc/deployment-apps
sudo mv " "
sudo mv " "
sudo mv " "

# --- Once all of the file shave been moved from /tmp to the /deployment-apps directory, then chown the deployment-apps    
     directory ---

sudo chown -R splunk:splunk /opt/splunk/etc/deployment-apps
# --- Exit out of sudouser ---
# --- Navigate to the bin directory ---
cd /opt/splunk/bin/
# ---Issue reboot command ---
./splunk restart
I've got a script located at:

I'm trying to invoke the script using:  

When the script runs it gives me this message:
Desktop/cfrtl4_all_indexer_base: No such file or directory

A portion of my script is below:
# ----- Variables -----
# ----- Script -----

# --- Search Head ---
sudo scp -r Desktop/Distributed_Lab/Apps/cfrtl4_all_indexer_base username@$SHIP:/tmp

Open in new window

I'm initiating the script from my local system where I have administrative rights.  However, I've tried the scp command with sudo and without.  Same result.  It keeps giving me this message:

"Desktop/cfrtl4_all_indexer_base: No such file or directory"

Why is the message referring to only the Desktop/ as the path and not the entire path?  More importantly, it's not seeing the file that I need to move from my Apps directory.  I created this script to move about 10 files over from my Desktop/Distributed_Lab/Apps directory which is on my local drive, on to a remote systems /tmp directory.   Any help is greatly appreciated!
How do i do a FULL directory search on a Linux box.  I want to look at the ENTIRE directory structure for a file.  In this example, I am looking for a file by the name of "email_log".

I am currently using:

find / -type f -name "email_log" and I've used sudo find / -type f -name "email_log" too, but when i hit enter, it wants the sudo password, which I enter in and it doesn't find anything. Is my search syntax incorrect?
I have a text file with 4 columns where the columns are delimited by a pipe character. The 3rd column, which is between the 2nd and 3rd pipe characters, may or may not have an extra pipe character.  I would like to replace these extra pipe characters in the 3rd column with a space.

File example:


In the 2nd line, in the 3rd column there is an extra pipe character. I would like to replace it with a space so that all lines have the same number of pipe characters. Any ideas as to how I could do it?
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
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.
Python 3 Fundamentals
LVL 19
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

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.