Friday, May 10, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 536  / 1 Year ago, wed, february 8, 2023, 11:01:55

I have this code snippet I am working on for a small project I am doing:



echo "Please enter SUID: ";
read $uid

uid_Assoc(){
arg1=$1
echo $1;
}

uid_Assoc


Now to start, I would like this to prompt for a string and read the string. From there I would like to pass that string as an argument and echo it out. But I am not sure how to pass the string off as a parameter. When I attempt to do something like:



uid_Assoc($uid)


It throws a syntax error (I figured that would be incorrect anyways). Are there any suggestions on how to do this?



UPDATE: I also tried re-arranging the code a bit to look like this:



function uid_Assoc(){
arg1=$1
echo $1;
}
echo "Please enter SUID: ";
read $uid
uid_Assoc $uid


But that didn't seem to work either.


More From » command-line

 Answers
0

Modify your script as follow:



#!/bin/bash

echo "Please enter SUID: ";
read uid

uid_Assoc () {
arg1="$1"
echo "$1"
echo "$arg1"
}

uid_Assoc "$uid"


You just need to remove the $ prefix before uid in the read call.


[#24600] Friday, February 10, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
suitman

Total Points: 487
Total Questions: 105
Total Answers: 98

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
;