靶场:https://www.hackerrank.com/
Task 1:
Let’s Echo
Write a bash script that prints the string “HELLO”.
1 | echo HELLO |
A Personalized Echo
Write a Bash script which accepts name as input and displays the greeting “Welcome (name)”
1 | read NAME |
read从标准输入中读取一行,并把输入行的每一个字段的值指定给后面的变量
或
1 | echo -n "Welcome " |
1 | printf "Welcome " |
echo和printf的区别:
echo:在标准输出上输出传递过来的内容,默认换行,加-n不换行,加-e 输出特殊字符
printf:与C类似.
printf "%s\n" we rty uio asd fgh
输出每个段都会换行
或
1 | echo "Welcome $(</dev/stdin)" |
每当在bash中使用类似’ $(command)这样的东西时,您就为该命令创建了一个子shell。输入是在子shell中获得的,输出将替换原始shell中的整个$(command) ‘部分。实际上,可以把它看作是先执行一个命令,然后将输出放到括号内的部分。
现在,</dev/stdin只是从子shell重定向输入。因此,无论在子shell中输入什么(来自测试用例的示例输入)都将被发送(并替换)$(</dev/stdin)
或
1 | sed -r 's/(.*)/Welcome \1/g' |
1 | xargs -I ^ echo "Welcome" ^ |