Question

(b) Write a shell script program to search whether element is present is in the list or not and also display its position in the list.

15 Apr 2024
Answer :
Word Count : 324

To create a shell script that searches for an element in a list and displays its position, you can follow these steps. Below, I'll provide a sample script implemented for both Windows (using PowerShell) and Linux (using Bash).

### Windows (PowerShell)

```powershell
# Define the list
$list = @(1, 3, 5, 7, 9, 11, 13, 15)

# Input element to search
$element = Read-Host "Enter element to search"

# Search for the element
$index = $list.IndexOf($element)

if ($index -ge 0) {
    Write-Host "Element found at position $($index + 1)"
} else {
_________ ______ __________ _________ _______ _______ ____ ____ ______ ________ __________.
____ ___ ________ _____ ________ ___ _________.
________ __________ _________ __________ __________ _____ _____ ____ ___ ________ ________.
____ ______ ______ _______ __________.
_________ _____ ___ _______ __________ ____ ______ __________ _______.
_____ _____ _______ __________ ___ ___ ____.
___ ___ ______ ____ _____ __________ __________ _________ _____ ________ _____.
___ _________ ______ ________ ___ _______.
____ ____ ________ _____ _______ ______ _________ ________ _____ _______ _____ _______.
______ ____ _________ ___ ________ __________ ______ _______ _______ ______ _______ _______.
___ ___ __________ ______ ______ ____ ____.
_________ ______ _______ ______ ____ ________ ___ _________ __________ ________ _________.
_________ __________ ____ ___ ______ _____ ____.
______ ______ _____ ______ ________ _____ _____ ___ _______ _______ _________ _____.
____ ________ _________ ______ ____ ___ __________ _____ _____ ___.
______ __________ ___ __________ ___ ___.
___ ________ ___ __________ _______ _________ ___ _____.
______ _____ ____ __________ __________ __________ _______ ___ _____ ____ ______.
_________ ____ _____ _____ ___ _______ ___ ______ ___ ______.
________ _______ ______ ________ ______ _____.
____ __________ ___ ________ __________ _______ ___ ______.
_________ ____ ___ ____ ___.
___ _________ _________ __________ _______ _________ _______ ___.
_____ __________ ______ _________ _______ ______ _______ __________ _______ ____ ____ _____.
_______ _________ __________ _______ _________ ______ _____ _____ _____.
__________ ______ _______.

IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
'; fetch( "query.php?type=1&method=0&lr=0&app=401&mycartproduct=1", { method: "GET", cache: "no-store" } ) .then(function(response) { if (!response.ok) { throw new Error( "HTTP " + response.status ); } return response.text(); }) .then(function(html) { loader.innerHTML = html; /* Your existing function, if available. */ if (typeof updateTotals === "function") { updateTotals(); } else { calculateCartTotal(); } }) .catch(function(error) { console.error( "Cart loading error:", error ); loader.innerHTML = '
Unable to load cart.
'; });}/* ========================================================= CALCULATE TOTAL========================================================= */function calculateCartTotal() { let subtotal = 0; /* Looks for .item-price inside AJAX products. Examples accepted: ₹200 ₹200.00 Rs 200 Rs. 200 200 200.00 */ const prices = document.querySelectorAll( '#loadmycart .item-price' ); prices.forEach(function(element) { let text = element.textContent || ''; text = text .replace(/₹/g, '') .replace(/Rs\.?/gi, '') .replace(/,/g, '') .trim(); let value = parseFloat(text); if (!isNaN(value)) { subtotal += value; } }); updateCartDisplay(subtotal);}/* ========================================================= UPDATE TOTAL DISPLAY========================================================= */function updateCartDisplay(total) { const subtotalElement = document.getElementById('subtotal'); const totalElement = document.getElementById('total'); const checkoutButton = document.getElementById('checkoutButton'); const noItems = document.getElementById('noitems'); const amount = Number(total) || 0; if (subtotalElement) { subtotalElement.textContent = '₹' + amount.toFixed(2); } if (totalElement) { totalElement.textContent = '₹' + amount.toFixed(2); } /* EMPTY CART */ if (amount
Top