We help IT Professionals succeed at work.

Shell Scripting

10K

Solutions

6K

Contributors

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.

HI,
I perform SSH to a remove linux server with a private key. Since I have a large pool of servers to ssh. I have also have two private keys used by these servers. but only subset uses the 2nd private key.

My request now is
I would like to add a logic in my calling shell script... if ssh failed with below error. Try with the 2nd private key. please help
Guessing, should handle the return code and determine to use 2nd key.

Permission denied (publickey,keyboard-interactive)

Open in new window


and this is how I connect
ssh -o StrictHostKeyChecking=no -i ~/.ssh/firstkey.pem  ec2-user@${targetIP}

Open in new window

0
Hello,

How can I add hostname on the below script :
#!/bin/bash
machine=`echo $hostname`

ip -f inet addr show dev eth0  |\
awk '{
    if ( "inet" == $1 )
        {
                 printf( "%s\n", substr( $2, 0, index($2,"/")-1) );
        }
        else
        {
                 printf( "%s ", substr( $2, 0, index( $2, ":")-1) );
        }


}'
ip -f inet addr show dev eth1  |\
awk '{
         if ( "inet" == $1 )
        {
                 printf( "%s\n", substr( $2, 0, index($2,"/")-1) );
        }
        else
        {
                 printf( "%s ", substr( $2, 0, index( $2, ":")-1) );
        }

}'

Open in new window


Thank you

Best regards
0
Dear Experts,

Can you please help me with this one?
i have below snippet...
I want to extract

session = MMECHJ-1
and
uproc =MMECHJ-1H


prompt>cat a
#!/usr/bin/ksh
set -x

LINE="MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904"

if [[ $(echo $LINE | awk -F"-" '{ print $1"-" $2 }') == "MMECHJ-1" ]] ; then
session=$(echo "$LINE" | awk -F"-" '{ print $1"-"$2 }')
uproc=$(echo "$LINE" | awk -F"-" '{ print $3"-"$4 }' | awk -F";" '{ print $1 }')
fi


echo "$session"
echo "$uproc"

Open in new window



Below how it works out:
awk -F"-" '{ print $1"-" $2 }' results in "MMECHJ-1 MMECHJ" , while seperator is the dash ;;.

prompt>./a
+ LINE=MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904
+ awk -F- { print $1"-" $2 }
+ echo MMECHJ-1 MMECHJ-1H P_PM43CEXP EXECUTING 19/03/2020 205814 19/03/2020 205814 LBKU01G SYS_BATCH 000000 100 0001093 0014704 19/03/2020 root 000 000 00 0013904
+ [[ MMECHJ-1 MMECHJ == MMECHJ-1 ]]
+ echo

+ echo

[bex1000@hn5219] /crelan/exp/prod/teste_production/teste_pascal/INC >

Open in new window


What's happening? This is ksh on aix.
:-~
Thanks again,
W.
0
hi,

in redhatl linux and AIX unix, how to use command to check:

1) CPU used ?
2) RAM used and left
3) diskspace used and left

how to add volume using command  ?

anyway to use Graphic mode to present that in linux without monitoring tools ?
0
I'm trying to get each line or row of output I get from a command into a seperate variable.
I'm defining a variable from this command I'm running (function listed below):
$PKGID = CM -Computername computer1 | Where-Object SuccessOrFailureCode -ne 0 | Select-Object packageid -ExpandProperty packageid
Write-output "$PKGID"

Open in new window

For example, if I only get one hit or SuccessOrFailureCode -ne 0, the output I get is:
1000
If I get two or more hits, the output can look like:
1001
1002
1003
I am then using this $PKGID variable in another function to trigger an install.
This only works when I have one hit, when I have more than one, the $PKGID variable encapsulates all of them and I cannot use.

Is there a way to get the output of each row into a seperate variable?  Example:
$PKGID would = 1000
$PKGID1 would = 1001
$PKGID2 would = 1002

This is the function I'm calling this from:

Function CM {

[CmdletBinding()]
Param
(
      [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
[string[]]$ComputerName = $env:COMPUTERNAME
)

Begin
{

$Code = {
# Get Execution History from registry, and package details from WMI
$ExecutionHistoryKey = "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client\Software     Distribution\Execution History" 
$ContextKeys = Get-ChildItem $ExecutionHistoryKey | Select -ExpandProperty PSChildName
foreach ($ContextKey in $ContextKeys)
{
    If ($ContextKey -eq "System")
    {
        $ContextKey = 

Open in new window

0
Hi all, I have a problem here with Linux Debian shell script, I am trying to create multiple users at once, but I kept facing the problem of "unrecognized option --stdin" how do I solve this? thanks in advance!
here is my script

for a in 'seq 1 100'
do
useradd user$a
echo password$a | --stdin user$a
done
0
given this line in a .csv file:
"RELAY.LDAP.PASSWORD","ENC(pbUfLNHe/GZs/owb0yFf5p6lEUKNDTIpckC7vHxHaXvE3bYFewA==)","aws-deu-emea"
Note: I have about 30k lines like this one it needs to go thru, but only 67 have the text I need to change

I am unable to change this value: aws-deu-emea . using my sed expression below
sed 's@"aws-deu-emea"@"aws-us-prod"@g'
Not sure what I am doing wrong, it should work, but it just run thru thru it, and not making any changes to any line

I have also tried the command with -i which results in:
sed: -i may not be used with stdin
0
Trying without success to create a bat file to copy a file creating a backuip version.

 If possible I'd like the copy of the file created with the data and time stamp at the time of creation.

For example: Original File name 'GRBDQDb01_2013.accdb'  Copied file name 'GRBDQDb01_2013_20200221_1100.accdb'

If that isn't possible I'd like the copied file to create anew version rather than overwrite an existing file of the same name.

For example: Original File name 'GRBDQDb01_2013.accdb'  Copied file name 'GRBDQDb01_2013_X.accdb', where X is the number of version that exist.

The file I want to copy is named 'GRBDQDb01_2013.accdb'.

It is in directory 'I:\My Documents\Access_Databases\GRBLaw\GRBDelinq'

This is what I have so far but it doesn't copy the file, let alone stamp it with the data and time or create anew version.

C:
cd I:\My Documents\Access_Databases\GRBLaw\GRBDelinq
copy I:\My Documents\Access_Databases\GRBLaw\GRBDelinq\GRBDQDb01_2013.accdb I:\My Documents\Access_Databases\GRBLaw\GRBDelinq\GRBDQDb01_2013_PreCompress.accdb 

Open in new window

0
Dear Experts,

I need 2 files compared on columns 13 and 14 (example: first line of first file > 3405143 5954504 ), and the differences written in seperated files.

If in file1 and not in file2 then write that line to "gone.txt" file.
If in file2 and not in file1 the write that line to "new.txt" file
If in file 1 and in file2 write that line in "stillthere.txt" file

file1
IN0R0300 IN0R0301_P P_IN56CEXP ABORTED 02/21/2020 065000 02/21/2020 070258 LBKL203 SYS_BATCH 000000 100 3405143 5954504 02/21/2020 epk 000 000 00 5407344 Launching Thaler XNINMLT MLTL_F1344914 W6 0000
IN0R0040 IN0R0041_P P_IN56CEXP ABORTED 02/21/2020 090623 02/21/2020 091224 LBKL203 SYS_BATCH 000000 100 3405585 5956805 02/21/2020 epk 000 000 00 5409565 file MBRS_20200221060228494_be07001648317966.edi 0000
IN0R0040 IN0R0041_P P_IN56CEXP ABORTED 02/21/2020 091344 02/21/2020 091458 LBKL203 SYS_BATCH 000000 100 3405615 5956984 02/21/2020 epk 000 000 00 5409747 file MBRS_20200221060234621_be02001352669040.edi 0000
IN0R0040 IN0R0041_P P_IN56CEXP ABORTED 02/21/2020 091714 02/21/2020 092555 LBKL203 SYS_BATCH 000000 100 3405628 5957067 02/21/2020 epk 000 000 00 5409826 file MBRS_20200221060308372_be46001027379136.edi 0000

Open in new window


file2
FT0Y7100 FT0Y7100_P P_FT56CEXP ABORTED 02/21/2020 000002 02/21/2020 000002 LBKL203 SYS_BATCH 000000 100 3399102 5948246 02/21/2020 epk 000 000 00 5401245 chain's name: FT0Y7100 0000
IN0R0300 IN0R0301_P P_IN56CEXP ABORTED 02/21/2020 065000 02/21/2020 070258 LBKL203 SYS_BATCH 000000 100 3405143 5954504 02/21/2020 epk 000 000 00 5407344 Launching Thaler XNINMLT MLTL_F1344914 W6 0000
ZMKTRCOD ZMX0850001 T_TH56CEXP ABORTED 02/21/2020 155227 02/21/2020 155227 LBKL203 SYS_BATCH 000000 100 3406949 5963705 02/21/2020 epk 000 000 00 5416287 chain's name: ZMKTRCOD
IN0R0310 IN0R0311_P P_IN56CEXP ABORTED 02/21/2020 160001 02/21/2020 160246 LBKL203 SYS_BATCH 000000 100 3406974 5963820 02/21/2020 epk 000 000 00 5416396 Launching Thaler XNIN109 IML_F1347154 W2 0000

Open in new window


Many thanks.
W
0
We have a shared network folder with over 400,000 loose files and need to create a powershell script that will take those files and move them into folders in the same directory based on the last three characters of the filenames.

For example:

[\\servername\fileshare\file001.eps] -> [\\servername\fileshare\001\file001.eps]
[\\servername\fileshare\file002.eps] -> [\\servername\fileshare\002\file002.eps]
[\\servername\fileshare\file400001.eps] -> [\\servername\fileshare\400001\file400001.eps]
[\\servername\fileshare\file12345.eps] -> [\\servername\fileshare\345\file12345.eps]

I am newbie to powershell scripting and any assistence would be greatly appretiated.

Thanks,
0
error starting ubuntu vm
ubuntu
i did this before i restartvm
https://help.ubuntu.com/community/Oracle%20Instant%20Client
0
I need help to write shell scripting to generate random number between (0,2147483647) infinite loop, random number will change every run during loop
0
Hi Linux experts,
could you help what is the best way to quickly or fastest way to find files with number range please. Basically need to copy files between 4000 and 5000 range.

Since this directory is huge, so my below command takes very long time to execute :)

for i in My_liferay_{4000..4005}*csv; do echo $i; done

Files
My_liferay_4000_1_51.csv
My_liferay_4001_1_52.csv
My_liferay_4002_1_53.csv
.
.
My_liferay_5000_1_60.csv

Open in new window

0
I need assistance to write shell script to resolve guess number game, called “guess.sh" this shell script will use  guess2  program  which written by C Program to generate a secret number and shell script “guess.sh” will ask user to guess a secret number which generated by guess2 program .
0
Hi,

If I write to a logfile in powershell and open it with cmtrace, it is locked.
How can I avoid this?

J
0
Hi
I have a comma separated values in two variables, would like to compare both variables and get values which are available in both variables.

leftString="ami-06bc7b1fd6d88be49,ami-04db3b64b5f3990dd,ami-0063dd4d697377715,ami-01f8ea3141121cda4,ami-083a15980f7fcf2ad,ami-0b22b5f710211"
rightstring="ami-023d322923f1bb271,ami-0390dfc7d966ce65d,ami-04db3b64b5f3990dd,ami-05f492333c31e3971,ami-066e692403bb98cd3,ami-06bc7b1fd6d88be49,ami-07ebe2d3da06f1831,ami-0a621c6a67fc67a3f,ami-0afa5ade0d61726ef,ami-0b41712d2aef56be1,ami-0f0b1e112bdf5034f"

Open in new window


what we are expecting in the final variable as below..since they are in both.
compareString="ami-06bc7b1fd6d88be49,ami-04db3b64b5f3990dd"

Open in new window


thanks in advance
0
Hi experts.

could you kindly help....how to get current date - 45 days in below format please

Basically would like to go back 45 days

YYYY-MM-DD
0
I am running into issue to determine escape characters from master to remote server over ssh.  I can return desired output on remote server with no issues.  Trying to run similar string on master server to remote server to get same result.

Below example is working fine on remote server and has expected results:

[root@remote_server ~]# echo ${passwd_expire}
Jun 17, 2020
[root@remote_server ~]# echo ${current_date}
Jan 09, 2020
[root@remote_server ~]# echo $(( ($(date --date="$(echo ${passwd_expire})" +%s) - $(date --date="$(echo ${current_date})" +%s) )/(60*60*24) ))
159

Open in new window


I would like to run those remotely and get same output, but I keep running into bash errors and I can't determine which escape character I could be missing or if it's execution process order is incorrect.

server=remote_server


[root@master ~]# echo ${passwd_expire}
Jun 17, 2020
[root@master ~]# echo ${current_date}
Jan 09, 2020
[root@master ~]# ssh -q ${server} "echo $(( ($(date --date="$(echo ${passwd_expire})" +%s) - $(date --date="$(echo ${current_date})" +%s) )/(60*60*24) ))"

Open in new window


Need some advice on how I can run similar string directly on remote server on this master server as well via ssh.
0
Hi Experts,

we have an in ternatinal network.
Do you know a way, how to determine all my DHCP servers in the whole network ?
0
I remotely connect to Linux virtual box from secure shell (ssh).  I have smallest bitbake project.

I type bitbake from shell prompt.  
The output is as follows:
Nothing to do.  Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information.

Bitbake document says default build task will be executed.
build default task is in Base.bbclass file.

How can I be sure this task actually executed?  How do I print something to the screen from this task?
base.bbclass
bblayers.conf
bitbake.conf
layer.conf
0
Hi Bash experts,

Trying to parse multiple xml files in a directory.
Extract an attribute value from available xml files. e.g liferay-version="20.0.1.5"
sort them and get the greatest value from the identified list. e.g "20.0.1.8" and store them in a variable

Was trying using python, but due to restriction on my work server. couldn't install additional packages, so couldn't use python.

Is it possible using sed/awk/bash or any way please.

file1.txt
<?xml version="1.0" encoding="utf-8"?>
<package date-created="26/11/2019" liferay-version="20.0.1.5" patch-version="5.0">
  <description />
  <system />
</package>

Open in new window


file2.txt
<?xml version="1.0" encoding="utf-8"?>
<package date-created="26/11/2019" liferay-version="20.0.1.6" patch-version="6.0">
  <description />
  <system />
</package>

Open in new window



file3.txt
<?xml version="1.0" encoding="utf-8"?>
<package date-created="26/11/2019" liferay-version="20.0.1.7" patch-version="7.0">
  <description />
  <system />
</package>

Open in new window


file4.txt
<?xml version="1.0" encoding="utf-8"?>
<package date-created="26/11/2019" liferay-version="20.0.1.8" patch-version="8.0">
  <description />
  <system />
</package>

Open in new window

0
I am trying to generate a report that will include the total mailbox quota for all my users.  When I use this string in the exchange admin console:Get-MailboxStatistics -server XXXXXX | Sort-Object TotalItemSize -Descending | fl displayname,database*quota,totalitemsize, I don't get the information I need.  A few of my users have larger quota's set for them.  According to the report that gets generated by using that string, it says that their databaseprohibitsendquota is set to 2GB, but the mailbox prohibit send is set to 4.88GB when I check by logging into the exchange admin console.  Is there a parameter that will get me total mailbox quota, not the database quota?
0
Hi bash experts,
would like to convert below log file separated by | into a nice table within my bash script. Is it possible? please help

lifray-portal-dev|464646464efef9||0|sometext
lifray-admin-dev|464646464efef9|ami-987654321|1|sometext
lifray-portal-test|464646464efef9||0|sometext
lifray-admin-test|464646464efef9||0|sometext
lifray-portal-sit1|464646464efef9||0|sometext
lifray-admin-sit1|464646464efef9|ami-987654321|2|sometext
lifray-portal-ppte|464646464efef9||0|sometext
lifray-admin-ppte|464646464efef9||0|sometext


found few links, but looks bit complex
https://www.unix.com/shell-programming-and-scripting/109997-csv-table-one-liner.html?_sm_au_=iqVvmlnQVN3VDHZrBJ3vvK7RJCBJt

https://unix.stackexchange.com/questions/105501/convert-csv-to-html-table?_sm_au_=iqVvmlnQVN3VDHZrBJ3vvK7RJCBJt
0
I know an ex-colleague has a way at command line (script or whatever) to automate
adding of IP to block malicious IP for Nokia Checkpoint : that's years ago.

My current network colleague says it's very tedious to add IP as he has to create
object, then go into another screen to add it to a group & we often get 100-700
IP from threat Intel (from a cyber regulator):  is there a way to automate to mass
block it for CheckPoint  Security Gateway 12600??     Isn't there a way to get to
SG12600's Unix command prompt & write a script to automate?


For sure Linux iptables, we can do it easily by Shell script.

Heard Palo Alto has an interface to add IP en masse but my network guy says
CheckPoint (& possibly Fortigate) don't.
0
Dear Experts,
I reckon you can help me, I'm struggling with assigning a variable in heredoc.
Everything works fine until I want to assign the pid value to a variable...

Script:

#!/bin/bash
set -x

#function
GetLine() {
ssh -T myuserid@"$server" <<EOF
sudo su - "$side""$container"
echo "$side""$container"
pgrep "$side""$container"
pid=$(pgrep "$side""$container")
echo "pid = "$pid""
exit
EOF
}


env=PRD
side=bas
for server in sv-com-bas-p1 sv-com-bas-p2
do
        suffix=$(echo $server | awk -F"-" '{ print $4 }')
        for container in cbs xcf
        do
        :
        GetLine
        done
done

Open in new window




Output:


+ env=PRD
+ side=bas
+ for server in sv-com-bas-p1 sv-com-bas-p2
++ awk -F- '{ print $4 }'
++ echo sv-com-bas-p1
+ suffix=p1
+ for container in cbs xcf
+ :
+ GetLine
+ ssh -T cgk_tam@sv-com-bas-p1
++ pgrep bascbs
bascbs
24426
pid =
+ for container in cbs xcf
+ :
+ GetLine
+ ssh -T cgk_tam@sv-com-bas-p1
++ pgrep basxcf
basxcf
26669
pid =
+ for server in sv-com-bas-p1 sv-com-bas-p2
++ awk -F- '{ print $4 }'
++ echo sv-com-bas-p2
+ suffix=p2
+ for container in cbs xcf
+ :
+ GetLine
+ ssh -T cgk_tam@sv-com-bas-p2
++ pgrep bascbs
bascbs
14642
pid =
+ for container in cbs xcf
+ :
+ GetLine
+ ssh -T cgk_tam@sv-com-bas-p2
++ pgrep basxcf
basxcf
16769
pid =



I know I most probably have to comment the variables in the pid=$(pgrep "$side""$container") line but whatever combination I tried was unsuccesful.
What am I missing?.
Thanks for your help.
0

Shell Scripting

10K

Solutions

6K

Contributors

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.

Top Experts In
Shell Scripting
<
Monthly
>